C# Access Modifier


Different Access Modifiers in C# are Public, Private, Protected, internal, protected internal.Default access modifier for members of class is private.

Default Access Modifiers in C#

 Namespace will not have access modifier.
 Default access modifier for class ,struct, Interface, Enum, Delegate  is Internal.
 Default access modifier for class and struct members is private.
 No access modifier can be applied to interface members and always interface members are public.
 Enum members are always public and no access modifier can be applied.
 

Access Modifier Basics

An access modifier is a keyword of the language that is used to specify the access level of members of a class. C#.net supports the following access modifiers.


  Public: When Members of a class are declared as public, then they can be accessed

 

1.    Within the class in which they are declared.

 

2.    Within the derived classes of that class available within the same assembly.

 

3.    Outside the class within the same assembly.

 

4.    Within the derived classes of that class available outside the assembly.

 

5.    Outside the class outside the assembly.


Internal: When Members of a class are declared as internal, then they can be accessed

 

1.    Within the class in which they are declared.

 

2.    Within the derived classes of that class available within the same assembly.

 

3.    Outside the class within the same assembly.



Protected: When  Members  of  a  class  are  declared  as  protected,  then  they  can  be accessed

 

1.    Within the class in which they are declared.

 

2.    Within the derived classes of that class available within the same assembly.

 

3.    Within the derived classes of that class available outside the assembly.




Protected internal: When Members of a class are declared as protected internal, then they can be accessed

 

1.    Within the class in which they are declared.

 

2.    Within the derived classes of that class available within the same assembly.

 

3.    Outside the class within the same assembly.

 

4.    Within the derived classes of that class available outside the assembly.




Private: Private members of a class are completely restricted and are accessible only within the class in which  they are declared.









protect c# c access modifiers public private protected c# c# class modifiers c# public private protected c# default access modifier c# public protected private protected c# internal protected c# c# protection c# access modifier protected internal modifier in c# c# protected default access modifier c# c# internal protected access modifier in c# c# modifiers access modifiers in c# access modifiers access modifier Default access modifiers