4

I have the following code:

public class PersonInitializer
{
    private Person _person;

    public static Person LoadFromFile(string path)
    {
        PersonInitializer x = new PersonInitializer();

        Person p = x._person; //Why am I accessible?

        return x.LoadFromFile(); //Sure.

    }

    public Person LoadFromFile(string path)
    {

    }
}

Why is _person accessible from x even if it is private? What can I do to "protect" _person?

Ian
  • 5,625
  • 11
  • 57
  • 93

5 Answers5

12

It is accessible, because you are the class it is defined in!

Access modifiers apply to classes, not to instances of a class. That means, an instance of class A has access to all private members of another instance of class A.

I assume, you agree with me, that this is ok:

var p = this._person;

But what about this:

public void DoSomething(PersonInitializer personInitializer)
{
    var p = personInitializer._person;
}

According to your assumption, this code would be valid depending on the input.
Example:

DoSomething(this); // ok
DoSomething(other); // not ok

This makes no sense :-)

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Scope based on instances of a class does make sence, Effel does it. It just lost out to the C++ mindset that lead to C# scopeing based on classes. – Ian Ringrose Mar 09 '11 at 12:44
3

This is because you are accessing it from a member function. If you want to prevent access from that particular function, you may want to move that static function to a new class.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
3

From the documentation:

Private members are accessible only within the body of the class or the struct in which they are declared.

Since LoadFromFile is within the body of the class where _person is declared, it has access to it. There's nothing you can do about that, since

Private access is the least permissive access level.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

Private variable's/references are accessible only in the class in which they are defined.
Since in you case, the reference Person _person; is defined in the same class from where you are accessing it, it is accessible.

Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
0

In C# (and Jave, C++) the scope of a field is based on the class, so all instances of the class can access private members of other instances of the same class.

In languages like Eiffel (and Smalltalk) the scope of a field is based on the instance, so a private field can only be access by the same instance. The Eiffel method may be better, but C++ won the hearts and minds of most programmer, hence very few people question “class based scopeing”

Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
  • 1
    Ian, you cannot make something so private that not even the same class can use it, sorry but the visibility accessor **secret** was not invented yet, so you can use **private** and this means from other classes you cannot see and use private members. – Davide Piras Mar 09 '11 at 11:21