apocalypse
apocalypse

Reputation: 5884

Struct - access to private fields

Why does it not generate an error?
If i try change private field of this struct in main program file - it generates an error, but not in struct implementation.

public struct MyStruct
{
    private int privateField;

    public int MyField
    {
        get { return privateField; }
        set { if (value >= 0) privateField = value; else value = 0 }
    }

    public void SomeMethod (MyStyruct s)
    {
        s.privateField = 10; // no error here.  
    }
}

Upvotes: 4

Views: 8394

Answers (7)

Benjamin Cutler
Benjamin Cutler

Reputation: 239

Private members are restricted to the class or struct not the object. In this case, even though s is a different object from this, it still works.

Upvotes: 7

Osiris
Osiris

Reputation: 499

This is allowed because the passed in type is a MyStruct as well. This struct can access private members from a struct of the same type.

As others have pointed out, this behavior is the same for class types as well.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503859

Firstly, this has nothing to do with whether it's a struct or a class - or indeed whether it's a field or some other member.

Accessibility in C# is decided based on where the code is, not whether it's "this object" or another object.

Section section 3.5 of the C# specification for more details. For example, from 3.5.2:

The accessibility domain of a member consists of the (possibly disjoint) sections of program text in which access to the member is permitted.

[...]

If the declared accessibility of M is private, the accessibility domain of M is the program text of T.

Upvotes: 5

Douglas
Douglas

Reputation: 54917

The private modifier means that a member may only be accessed from within its type. It does not constrain access to from within its instance.

Upvotes: 1

Chris Shain
Chris Shain

Reputation: 51369

Because SomeMethod is a method on the struct itself. You wouldn't get an error on a class either- local members are allowed to modify private fields.

Upvotes: 1

Rob Levine
Rob Levine

Reputation: 41358

That behaviour is not specific to structs - it holds true of classes too.

private means the field is only accessible within the type. It is not meant to imply "only within the same instance of the type".

Given you are trying to access a private field from within the same type even though it is a different instance of the same type, private should allow access.

Upvotes: 4

Tony The Lion
Tony The Lion

Reputation: 63310

private means that it can only be used in the scope of the struct. It's an access modifier saying that the access level of this variable, function is private to the struct itself only.

You should use your property to change it outside of the struct, in main for example.

Your method SomeMethod is part of the struct, so you're allowed to change it's private members.

Although, the way you're accessing it, is a bit unusual.

Upvotes: 3

Related Questions