Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Access Modifiers in C# .NET with Examples

0.00/5 (No votes)
16 Oct 2015 1  
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.

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

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

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.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here