Thursday, February 10, 2011

Understanding Access Modifiers/Specifiers in C#


Access modifiers are keywords (private, public, internal, protected and protected internal) which are used to specify the accessibility of a type and its members.

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;
   }
}

Now to demonstrate the behaviour how these class members are exposed to another class depending upon their scope defined via modifier/specifier and when we create an object or inherit from the above created class named "AccessModifier"
As mentioned in the above shown code and before each member variable I wrote a comment which describes the access level of each type of member varaible.

So if we derive another class "CallAccessModifier" from the parent class "AcessModifier" then "private" type members will not be visible because its considered as outside the scope of parent class.

But "protected" members are the ones which become available only during the inheritance (when a child is derived from a parent class) as shown via image below.

Showing class members availability through inheritance.




















                         
Note:-
"this" is a keyword referes to the current instance of the class and used to access members. In the image above this keyword shows all the available members with the classs.

If you notice in the above shown image it clearly shows all the protected members are visible due to virtue of inheritance, along with other members with scope defined as internal, public and protected internal.

Now let's see what happens if we decide to create an object of the class "AccessModifier" shown in the code above. As per the rule private and protected must not be visible via an object:
Showing members available when an object is created of the given class
















                     
        
Now as shown in the image just above we are not able to see private and protected members because they both are not exposable via an object.

Besides we are able to see protected internal member, and this is correct because its a mix of both protected and internal and so exposed in here because we are in the same assembly.

Q. What happens if we build a .dll of the "AccessModifier" code and use it in other project.
A. The rules remain the same, once AccessModifier.dll is refered in other project its considered as outside ot the current assembly and so:
 ** private members are not exposed
 ** internal members are not exposed