Access Modifiers in c#


Access Modifiers

Access Modifiers define the rules for accessing the member of class from outside. Access modifier define the scope of any element i.e. define the scope to the data members and methods inside class.


There are 4 types of access modifier
1.       Public Modifier: As name suggested allows public access inside class and outside of class without any restrictions
2.       Internal Modifier: Allows access only in current assembly. If we access the member outside assembly it gives error
3.       Protected Modifier: Allows access in same class where it is declared and also in a class derived from this class in which they are declared.
4.       Protected Internal: Allow access to the current assembly, containing class in which members are declared and the class derived from containing class.

Key Points:

“One more thing about class, class has only public access modifier it cannot be private, protected and protected internal , It gives error “Error : Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal”

“Another thing is by default all members of a class are private”


Post a Comment

0 Comments