Understanding Access Modifiers in C-Sharp

Hi friends, In this article, we will understand Access Modifiers in C# with only on common example.

The type or member can be accessed by any code in the same assembly, but not from another assembly.

Step 1:   Create console application ConsoleApp1.

Step 2:   Add new ClassLibrary (.net framework) in same solution TestLibrary.

Step 3:   Add reference of TestLibrary in ConsoleApp1

Step 4:   Add new class in TestLibrary project TestLibBaseClass and copy following code.

namespace TestLibrary { public class TestLibBaseClass { //Data Member Declaration with access modifiers private int pvt; public int pub; protected int prot; internal int i; protected internal int pi; // Initialization using constructor public TestLibBaseClass() { pvt = pub = prot = i = pi = 0; } // Member function public System.Collections.Generic.ListTestLibBaseMethod() { System.Collections.Generic.List testlibvar = new System.Collections.Generic.List(); //Private Member Accessible With Declared Class same Assembly testlibvar.Add(pvt); //Public Member Accessible With Declared Class same Assembly testlibvar.Add(pub); //Protected Accessible With Declared Class same Assembly testlibvar.Add(prot); //internal Member Accessible With Declared Class same Assembly testlibvar.Add(i); //protected internal Member Accessible With Declared Class same Assembly testlibvar.Add(pi); return testlibvar; } } public class Derivedclass : TestLibBaseClass { public System.Collections.Generic.List DerivedOfTestMethod() { System.Collections.Generic.List testlibvar = new System.Collections.Generic.List(); //Private Member Not Accessible With Derived Class same Assembly //testlibvar.Add(pvt); //Public Member Accessible With Derived Class same Assembly testlibvar.Add(pub); //Protected Accessible With Derived Class same Assembly testlibvar.Add(prot); //internal Member Accessible With Derived Class same Assembly testlibvar.Add(i); //protected internal Member Accessible With Derived Class same Assembly testlibvar.Add(pi); return testlibvar; } } public class OtherClass { TestLibBaseClass t = new TestLibBaseClass(); //private Not Accessible With Other Class same Assembly //public int OtherClass_Private_Not_Access() => t.prv; public int OtherClassPublic_Access() => t.pub; //protected Not Accessible With Other Class same Assembly //public int OtherClass_Protected_Not_Access() => t.prot; //internal Accessible With Other Class same Assembly public int OtherClass_Internal_Access() => t.i; //protected internal Accessible With Other Class same Assembly public int OtherClass_Protected_Internal_Access() => t.pi; } }

EXPLANATION:

1) TestLibBaseClass Class

TestLibBaseClass class has Data members with private, public, protected, internal and protected internal access modifiers with data type int. One default constructor for initialization of data members and one public member function TestLibBaseMethod, with generic List collection of int i.e. List.

DDATA MEMBER SCOPE:

With in TestLibBaseClass class all data members are accessible with in its member function TestLibBaseMethod().

2) TestLibBaseClass Class

Derivedclass class which inherits TestLibBaseClass with in same assembly TestLibrary having one public member function DerivedOfTestMethod, with generic List collection of int i.e. List.

DDATA MEMBER SCOPE:

With in DerivedOfTestMethod member function all data member of base class TestLibBaseClass are accessible except one private member i.e. pvt.

3) OtherClass Class

OtherClass with in same assembly is simple Class. In which an local private object of TestLibBaseClass is created. and return its data members by different methods.

DDATA MEMBER SCOPE:

Here five methods are created. out of them public, internal and protected internal are accessible and private and protected are not accessible due to protection level.

Step 5:   Add new class in ConsoleApp1 project DerivedFromTestBase and copy following code.

using TestLibrary; namespace ConsoleApp1 { public class DerivedFromTestBase : TestLibBaseClass { public System.Collections.Generic.List DerivedMethod() { System.Collections.Generic.List testlibvar = new System.Collections.Generic.List(); //Private Member Not Accessible With Derived Class Other Assembly //testlibvar.Add(pvt); //Public Member Accessible With Derived Class Other Assembly testlibvar.Add(pub); //Protected Member Accessible With Derived Class Other Assembly testlibvar.Add(prot); //protected internal Not Accessible With Derived Class Other Assembly //testlibvar.Add(i); //protected internal Member Accessible With Derived Class Other Assembly testlibvar.Add(pi); return testlibvar; } } class Program { static void Main() { DerivedFromTestBase d = new DerivedFromTestBase(); // Only public method Accessible with Other Class Other Assembly System.Console.WriteLine(d.pub); } } }

EXPLANATION:

DerivedFromTestBase Class

DerivedFromTestBase Class, which is located in other assembly (ConsoleApp1), derived from TestLibBaseClass which is also created in different assembly (TestLibrary).

In DerivedFromTestBase Class, we created a public member function DerivedMethod, with generic List collection of int i.e. List. Each method have return type int. Each method will return data member of TestLibBaseClass.

DATA MEMBER SCOPE:

out of five access modifiers it will access only public, protected and protected internal data members.

protected and internal are not accessible due to protection levels.

Access Modifiers Same Assembly Other Assembly
Declared Class (Local) Other Class Derived Class Other Class Derived Class
PRIVATE YES NO NO NO NO
PUBLIC YES YES YES YES YES
PROTECTED YES NO YES NO YES
INTERNAL YES YES YES NO NO
PROTECTED INTERNAL YES YES YES NO YES
PRIVATE PROTECTED (C# 7.2) YES NO YES NO NO

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories