开发者

Class-behaviour with private fields

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 a开发者_开发知识库n 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!


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


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...


"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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜