C Sharp Access Modifiers / Specifiers

C# Access Modifiers / Specifiers

The keywords used in a C# application to specify the accessibility or scope of variables and functions are called C# Access modifiers or specifiers. There are five types of access specifiers in C#. These are:

  • Public
  • Protected
  • Internal
  • Protected internal
  • Private

Any of the specifiers can be used to protect the data, where, Private is the most restricted and Public is not restricted. The accessibility of each of these specifiers is described in the below table.

Access Specifier Uses
Public To define that the access is not restricted.
Protected To define that the access is limited to the containing class or in derived class.
Internal To define that the access is limited to the current assembly.
Protected internal To define that the access is limited to the current assembly or types derived from the containing class.
Private To define that the access is limited to the containing type.

C# Public Access Specifier:

For accessing the data publicly without any restriction of the data to the declared block, the public access specifier is used in C#.

Example:

using System;  
namespace Example  
{  
    class Hello  
    {  
        public string msg1 = "World!!";  
        public void Message(string msg2)  
        {  
            Console.WriteLine("Hello " + msg2);  
        }  
    }  
    class Hello2  
    {  
        static void Main(string[] args)  
        {  
            Hello hello = new Hello();  
 
            Console.WriteLine("Hello " + hello.msg1);  
 
            hello.Message("Vishal Gupta!!");  
        }  
    }  
}

Output:

Explanation:

In the above example, we are displaying the use and behavior of the public access specifier in C#.

C# Protected Access Specifier:

For accessing the data within the class with limited scope, the protected access specifier is used in C#. In the case of inheritance, the accessibility is also within the subclass or child class.

Example 1:

using System;  
namespace Example  
{  
    class Hello  
    {  
        protected string msg1 = "World!!";  
        protected void Message(string msg2)  
        {  
            Console.WriteLine("Hello " + msg2);  
        }  
    }  
    class Hello2  
    {  
        static void Main(string[] args)  
        {  
            Hello hello = new Hello();  
 
            Console.WriteLine("Hello " + hello.msg1);  
 
            hello.Message("Vishal Gupta!!");  
        }  
    }  
}

Output:

Explanation:

In the above example, we are displaying the use and behavior of the protected access specifier in C#.

Example 2:

using System;  
namespace Example  
{  
    class Hello  
    {  
        protected string msg1 = "World!!";  
        protected void Message(string msg2)  
        {  
            Console.WriteLine("Hello " + msg2);  
        }  
    }  
    class Hello2 : Hello 
    {  
        static void Main(string[] args)  
        {  
            Hello2 hello2 = new Hello2();  
 
            Console.WriteLine("Hello " + hello2.msg1);  
 
            hello2.Message("Vishal Gupta!!");  
        }  
    }  
}

Output:

Explanation:

In the above example, we are accessing the protected members within a child class by inheritance.

C# Internal Access Specifier:

For accessing the data within files in the same assembly the internal access specifier is used in C# and is specified by the internal keyword.

Example:

using System;  
namespace Example  
{  
    class Hello  
    {  
        internal string msg1 = "World!!";  
        internal void Message(string msg2)  
        {  
            Console.WriteLine("Hello " + msg2);  
        }  
    }  
    class Hello2  
    {  
        static void Main(string[] args)  
        {  
            Hello hello = new Hello();  
 
            Console.WriteLine("Hello " + hello.msg1);  
 
            hello.Message("Vishal Gupta!!");  
        }  
    }  
}

Output:

Explanation:

In the above example, we are displaying the use and behavior of the internal access specifier in C#.

C# Protected Internal Access Specifier:

To access the specifier within the assembly in which it is declared or within a derived class in another assembly, the variable or function declared protected internal access specifier can be used and is specified by the protected internal keyword.

Example:

using System;  
namespace Example  
{  
    class Hello  
    {  
        protected internal string msg1 = "World!!";  
        protected internal void Message(string msg2)  
        {  
            Console.WriteLine("Hello " + msg2);  
        }  
    }  
    class Hello2  
    {  
        static void Main(string[] args)  
        {  
            Hello hello = new Hello();  
 
            Console.WriteLine("Hello " + hello.msg1);  
 
            hello.Message("Vishal Gupta!!");  
        }  
    }  
}

Output:

Explanation:

In the above example, we are displaying the use and behavior of the protected internal access specifier in C#.

C# Private Access Specifier:

The private accessibility to the variable or function is defined by the Private Access Specifier in C# which is specified by the private keyword. Being the most restrictive specifier, it provides accessibility only within the body of the class in which it is declared.

Example 1:

using System;  
namespace Example  
{  
    class Hello  
    {  
        private string msg1 = "World!!";  
        private void Message(string msg2)  
        {  
            Console.WriteLine("Hello " + msg2);  
        }  
    }  
    class Hello2  
    {  
        static void Main(string[] args)  
        {  
            Hello hello = new Hello();  
 
            Console.WriteLine("Hello " + hello.msg1);  
 
            hello.Message("Vishal Gupta!!");  
        }  
    }  
}

Output:

Explanation:

In the above example, we are displaying the use and behavior of the private access specifier in C#.

Example 2:

using System;  
namespace Example  
{  
    class Hello2
    {  
        private string msg1 = "World!!";  
        private void Message(string msg2)  
        {  
            Console.WriteLine("Hello " + msg2);  
        }  
 
        static void Main(string[] args)  
        {  
            Hello2 hello2 = new Hello2();  
 
            Console.WriteLine("Hello " + hello2.msg1);  
 
            Hello2.Message("Vishal Gupta!!");  
        }  
    }  
}

Output:

Explanation:

Here, we are displaying another example of the private access specifier in C#.