All my college years I have been using public
, and would like to know the difference between public
, private
, and protected
?
Also what does static
do as opposed to having nothing?
All my college years I have been using public
, and would like to know the difference between public
, private
, and protected
?
Also what does static
do as opposed to having nothing?
I think it is related to good OOP design. If you are a developer of a library you want to hide the inner workings of your library. That way, you can modify your library inner workings later on. So you put your members and helper methods as private, and only interface methods are public. Methods that should be overwritten should be protected.
A graphical overview (summary in a nutshell)
For the defaults if you put no access modifier in front, see here:
Default visibility for C# classes and members (fields, methods, etc)?
Non-nested
Nested:
C# has in total 6 access modifiers:
private: The member declared with this accessibility can be visible within the containing type, it is not visible to any derived types, other types in the same assembly or types outside of the containing assembly. i.e., access is limited to the containing type only.
protected: The member declared with this accessibility can be visible within the types derived from the containing type within the containing assembly, and the types derived from the containing type outside of the containing assembly. i.e., access is limited to derived types of the containing type.
internal: The member declared with this accessibility can be visible within the assembly containing this member, it is not visible to any assembly outside of the containing assembly. i.e., access is limited to containing assembly only.
internal protected: The member declared with this accessibility can be visible within the types derived from the containing type within or outside of the containing assembly, it is also visible to any types within the containing assembly. i.e., access is limited to containing assembly or derived types.
public: The member declared with this accessibility can be visible within the assembly containing this member, or any other assembly that references the containing assembly. i.e., access is not limited.
C# 7.2 is adding a new level of accessibility:
private protected: The member declared with this accessibility can be visible within the types derived from this containing type within the containing assembly. It is not visible to any types not derived from the containing type, or outside of the containing assembly. i.e., the access is limited to derived types within the containing assembly.
Source including a sample code of the new private protected access modifier
Public - If you can see the class, then you can see the method
Private - If you are part of the class, then you can see the method, otherwise not.
Protected - Same as Private, plus all descendants can also see the method.
Static (class) - Remember the distinction between "Class" and "Object" ? Forget all that. They are the same with "static"... the class is the one-and-only instance of itself.
Static (method) - Whenever you use this method, it will have a frame of reference independent of the actual instance of the class it is part of.
Those access modifiers specify where your members are visible. You should probably read this up. Take the link given by IainMH as a starting point.
Static members are one per class and not one per instance.