OOP Series #1: Building and Consuming a Class Library (DLL) Using C#
Introduction
Access modifiers and specifiers are keywords (private, public, internal, protected and protected internal) to specify the accessibility of a type and its members.
C# has 5 access specifier or access modifier keywords; those are private, public, internal, protected and protected Internal.
Usage of Access Specifiers
private: limits the accessibility of a member to within the defined type, for example if a variable or a functions is being created in a ClassA and declared as private then another ClassB can't access that.
public: has no limits, any members or types defined as public can be accessed within the class, assembly even outside the assembly. Most DLLs are known to be produced by public class and members written in a .cs file.
internal: internal plays an important role when you want your class members to be accessible within the assembly. An assembly is the produced .dll or .exe from your .NET Language code (C#). Hence, if you have a C# project that has ClassA, ClassB and ClassC then any internal type and members will become accessible across the classes with in the assembly.
protected: plays a role only when inheritance is used. In other words any protected type or member becomes accessible when a child is inherited by the parent. In other cases (when no inheritance) protected members and types are not visible.
Protected internal: is a combination of protected and internal both. A protected internal will be accessible within the assembly due to its internal flavor and also via inheritance due to its protected flavor.
Code Sample
- public class AccessModifier
- {
- //Available only to the container Class
- private string privateVariable;
- // Available in entire assembly across the classes
- internal string internalVariable;
- //Available in the container class and the derived class
protected string protectedVariable;
//Available to the container class, entire assembly and to outside
public string publicVariable;
- //Available to the derived class and entire assembly as well
protected internal string protectedInternalVariable;
- private string PrivateFunction()
- {
- return privateVariable;
- }
- internal string InternalFunction()
- {
- return internalVariable;
- }
- protected string ProtectedFunction()
- {
- return protectedVariable;
- }
- public string PublicFunction()
- {
- return publicVariable;
- }
- protected internal string ProtectedInternalFunction()
- {
- return protectedInternalVariable;
- }
- }



Sameep BaxiPosted Aug 5, 2019, 6:16 AM
What if the class AccessModifier is set as private or protected, and class members as public?
Sekhar BabuPosted Jul 7, 2017, 11:37 AM
Thank You Vidya Sir.. clearly explained.
Manav PandyaPosted Jul 6, 2017, 3:03 AM
Nice share Vidya sir ......
Amit KumarPosted Jul 3, 2017, 11:44 PM
Nice explanation sir..
AhsanPosted Jun 28, 2017, 4:39 PM
Best article for a quick interview refresher :P
SubashPosted Sep 27, 2016, 3:46 AM
Very nice explanation sir
Bhuvanesh MohankumarPosted May 10, 2016, 2:29 AM
Nice one
Sr KarthigaPosted May 6, 2016, 9:19 PM
good one sir
Gowtham RajamanickamPosted May 20, 2015, 1:17 AM
good one
Vithal WadjePosted Oct 8, 2014, 3:17 PM
great one ,keep it Up