Monday 15 June 2015

properties - Class-behaviour with private fields -


I have behaved weirdly in a library (at least for me);

I have this method:

  public class Lib {private string field = "field"; Public String Field {Get Return Field; }} Add Public Zero (Lib Lib) {string field = DoSomething (lib); Console.WriteLine (area); } Secure string DoSomething (Lib Lib) {return lib.field; }}  

So, if I say a program to a program using a library:

  lib lib = new Lib (); Lib Libib 2 = new lib (); Lib.Add (lib2);  

The console gives me the "field" as the output ... Now, I did not understand why this is why I used the area as private Announced, why can a class reach the private property of other classes and it does not give me an exception about access restrictions ?!

In my comprehension, a Lib can access its fields in any way, but when I give the law another example of Lib , Then it should not be possible to enter the private sector for the first time because .. OK, because this is another example and private!

It is by design, private access type with access modifier There is a limit No type of type Nessted type also has access to the private sector of the private type.

  class MyType {private string secret; Public MyType (String Secret) {this.secret = secret; } Public Zero Some Method (MyType myType) {// Works related to the Private Modifier type, not the Example Console.WriteLine (myType.secret); } Public category MyNestedType {Public MyNestedType (MyType myType) {// Also works because we are in the nested type of MyType Console.WriteLine (myType.secret); }}}  

relevant link:,


No comments:

Post a Comment