Saturday, October 3, 2015

Access modifiers in C#.NET with exampls

Introduction:

Access modifier are the defined level of permission to access properties and methods. By declaring this access modifiers we defining a variable or a event can be access from assembly to within that class. Lets see how.

Description:

There are 4 major access modifiers in C#. These are...

  1. Public
  2. Private
  3. Protected
  4. Internal
  5. Protected Internal
Public:
Using Public a event or a variable can be accessed from outside of  the class, where it belongs. And also from the outside of the assembly. 

class ClassTest
{
    //Public method
    public void MethodPublic()
    {
        // defination of MethodPublic
    }
}

// to access the method
class Program
{
    static void Main(string[] args)
    {
        ClassTest objClassTest = new ClassTest();
           
        objClassTest.MethodPublic(); // valid code to access.  
    }
}

Private:
It restricts the use of methods and variables only within the the class itself. It cant be used from the outside of the class. As you declare a private constructor of a class that class can't be access from the outside that class, you can't create an object of that class,

Example 1: Private keyword
class ClassTest
{
    //Private method
    private void MethodPrivate()
    {
        // defination of MethodPrivate
    }
}
// to access the method
class Program
{
    static void Main(string[] args)
    {
        ClassTest objClassTest = new ClassTest();

        objClassTest.MethodPrivate(); // invalid code to access.  
    }
}


Example 2: Private Constructor 
class ClassTest
{
    private ClassTest() { } // private constructor
}
// to access the method
class Program
{
    static void Main(string[] args)
    {
        // invalid code. can't create an object of this class
        ClassTest objClassTest = new ClassTest();
    }
}

Protected:
This allows variables  and methods to access from that class and the sub class of the class. That means that methods can be access within that class and from the classes, which actually inheriting that class. 

class ClassTest
{
    //Protected variable
    protected int _a;
}

class ClassTest2 : ClassTest
{
    ClassTest2()
    {
        this._a = 10; // can access from this class
    }
}

class ClassTest3
{
    ClassTest3()
    {
        this._a = 10; // can't access from this class
    }
}

Internal:
Internal is introduces in C#. In JAVA we don't have this access modifier. This allows the access after Protected. As Protected it is also allow to access the methods and variables from that class and the sub classes of that class. It added the assembly into it. That means the variables and methods can be access within the assembly where the class belongs. Now make that sure that here we are talking about Namespace, because Namespace and assembly are slightly different. An assembly can hold more than one Namespace. Assemblies are actually the dll of the project.

class ClassTest
{
    internal void MethodInternal()
    {
        // do your code
    }
}
// to access the method
class Program
{
    static void Main(string[] args)
    {
        ClassTest objClassTest = new ClassTest();

        objClassTest.MethodInternal(); // valid code to access.  
    }
}

Protected Internal:
Protected Internal allows you to access the variables and methods to access from that class and sub classes of that class. Also allows to access within the same assembly. Means in protected if the class is inheriting the super class and the method or variable is protected then the assembly doesn't matter to access. But in the Internal the assembly matters if the class is inheriting the super class. That is why we use Protected Internal access modifier. 

class ClassTest
{
    protected internal string name; // protected internal
    public void print()
    {
        Console.WriteLine("\nMy name is " + name);
    }
}
// to access the method
class Program
{
    static void Main(string[] args)
    {
        ClassTest objClassTest = new ClassTest();
        // Accepting value in protected internal variable
        objClassTest.name = "Arka";
        objClassTest.print();
    }
}

In sort:

Public: Any where from the class
Private: Only within the class
Protected: Only within that class and the sub classes of that class
Internal: Within the assembly of the class
Protected Internal: Within that class, sub classes of that class and and assembly.

Hope it will clear your concept about the access modifiers. Don't forget to post your valuable comments. 

0 comments:

Post a Comment

Follow by Email

Popular Posts

Pageviews