Click here to Skip to main content
15,888,521 members
Articles / Programming Languages / C#

Access Modifiers in C# .NET with Examples

Rate me:
Please Sign up or sign in to vote.
4.29/5 (14 votes)
16 Oct 2015CPOL2 min read 78.5K   14   3
Access modifiers in C#.NET with examples

Introduction

Access modifiers are the defined level of permission to access properties and methods. By declaring these access modifiers, we are defining a variable or an event can be accessed from assembly to within that class. Let's 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, an event or a variable can be accessed from outside of the class, where it belongs. And also from the outside of the assembly.

C#
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 class itself. It can't be used from outside of the class. As you declare a private constructor of a class, that class can't be accessed from outside that class, you can't create an object of that class,

Example 1: Private keyword

C#
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

C#
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 accessed within that class and from the classes, which actually inherit that class.

C#
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 introduced in C#. In JAVA, we don't have this access modifier. This allows the access after Protected. As Protected, it also allows 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 accessed within the assembly where the class belongs. Now make 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.

C#
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. This 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.

C#
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 short:

  • Public: Anywhere 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.

This article was originally posted at http://asp-arka.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer PwC
India India
I am a Software developer having an experience of 5 years in application development. To get me you can mail me at arkadeepde@gmail.com or you can visit my blog at ASP With Arka

Comments and Discussions

 
Generalmore info on protected internal Pin
John Torjo16-Oct-15 10:41
professionalJohn Torjo16-Oct-15 10:41 
GeneralRe: more info on protected internal Pin
Arkadeep De19-Oct-15 0:28
professionalArkadeep De19-Oct-15 0:28 
GeneralRe: more info on protected internal Pin
Member 1228851926-Jan-16 5:44
Member 1228851926-Jan-16 5:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.