c# - Why have class-level access modifiers instead of object-level? -


while using c#, realised can call foo object's private functions foo's static functions, , other foo objects. after have learned access modifiers, sounds strange me.

as far know, make function private when that's part of kind of internal process. object knows when use functions, because other objects shouldn't/can't control object's flow. there reason why other objects of same class should excepted pretty straightforward rule?

as per request, example:

public class aclass {     private void dosomething() { /* here */ }     public void afunction() {         aclass f = new aclass();         f.dosomething(); // have expected line cause access error.     } } 

when make member private, it's private other classes not class itself.

it can useful instance if have equals method needs access instance's private members:

public class aclass {     private int privatemembera;      // version of equals has been simplified     // purpose of exemplifying point, shouldn't copied     public override bool equals(object obj)     {         var otherinstance = obj aclass;         if (otherinstance == null)         {             return null;         }          return otherinstance.privatemembera == this.privatemembera;     } } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -