views:

31

answers:

3

I encoutered a somewhat (at least for me) strange behaviour in a library I'm building;

I have this method:

public class Lib
{
 private string field = "field";

 public string Field
 {
  get
  {
   return field;
  }
 }

 public void Add(Lib lib)
 {
  string field = DoSomething(lib);
  Console.WriteLine(field);
 }

 protected string DoSomething(Lib lib)
 {
  return lib.field;
 }
}

So, if I call the method from a program that uses the library:

Lib lib = new Lib();
Lib lib2 = new Lib();

lib.Add(lib2);

The Console gives me "field" as output... Now, I don't quite understand why that happens. I declared the field as private, so why can one class access the other classes' private property and it doesn't give me an Exception about access-restrictions?!

In my understanding, a Lib can access it's own fields anyways, but when I give the method an other instance of Lib, it should not be possible for the first instance to access the seconds' private fields, because... well, because it's an other instance and private!

+1  A: 

This is by design, the private Access Modifier limits the access to the containing Type. Not an instance of the Type. Nested Types also have access to private fields of the containing Type.

class MyType
{
    private string secret;

    public MyType(string secret) { this.secret = secret; }

    public void SomeMethod(MyType myType)
    {
        // Works because the private modifier pertains to the Type, not the instance
        Console.WriteLine(myType.secret); 
    }

    public class MyNestedType
    {
        public MyNestedType(MyType myType)
        {
            // Also works because the we are in a nested type of MyType
            Console.WriteLine(myType.secret); 
        }
    }
}

The relevant links: Accessibility Levels, private

Yannick M.
And how can you declare a field as `secret` to the instance of a class?
ApoY2k
There is no access modifier, that I know of, that does this.
Yannick M.
A: 

private means private to the class, not the instance.

Thought this was strange at first myself, and thought I had discovered a "bug" in .NET. Boy was I put back in place when I released that theory...

erikric
A: 

"The Console gives me "field" as output... Now, I don't quite understand why that happens. I declared the field as private, so why can one class access the other classes' private property and it doesn't give me an Exception about access-restrictions?!"

But the other class isn't accessing field directly; it's just accessing a public method of the instance of the Lib class, which in turn accesses the private member field (which it is allowed to do, since a class may access its own private members.

It's a bit like saying that you can't walk into my house and take my stuff, because that's private. However, you are very free to knock on my door and ask me to give you my stuff, because that's a public way of doing so.

Shoko
The method is accessing the private field: "field" from the parameterized instance of `Lib`.
Yannick M.
OK, I just noticed the "return lib.field;". I thought that I shouldn't be able to access private types of instances under ANY circumstance, but what you are saying seems to be that once the instance is used within the class definition (e.g. in a function), then its private fields can be accessed.
Shoko