C# Access Modifiers
C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#:
- Private
- Protected
- Internal
- Protected internal
- Public
Access Modifier | Description |
public | Public variable is accessible everywhere |
Protected | Protected is accessible only in derived class |
Internal | Internal is accessible only in containing assembly |
Protected internal | It is accessible in current assembly or derived classes. |
Private | It is only accessible within class |
Example:
using System; public class Student { int id,age; string name,course; int batch; public Student(int id,int age,string name,string course,int batch) { this.id=id; this.age=age; this.name=name; this.course=course; this.batch=batch; } public void display() { Console.WriteLine(id+" "+age+" "+name+" "+course+" "+batch); } } public class StudentDetails { public static void Main (string[] args) { Student s1=new Student(101,23,"Varun Kumar","B. Tech.",2017); s1.display(); } }Output
101 23 Varun Kumar B. Tech. 2017Protected Access Modifier: we can access protected modifier within the class or outside the class by inheritance.
Example:
using System; public class Student { int id,age; string name,course; int batch; public Student(int id,int age,string name,string course,int batch) { this.id=id; this.age=age; this.name=name; this.course=course; this.batch=batch; } protected void display() { Console.WriteLine(id+" "+age+" "+name+" "+course+" "+batch); } } public class StudentDetails { public static void Main (string[] args) { Student s1=new Student(101,23,"Varun Kumar","B. Tech.",2017); s1.display(); } }Output
Compilation error (line 26, col 6): 'Student.display()' is inaccessible due to its protection levelProtected Access modifier: Accessing data through inheritance
using System; public class Animal { int age=10; protected void eat() { Console.WriteLine("at the age of "+age+" it should eat only normal food"); } } public class Dog:Animal { public static void Main(string[] args) { Dog a=new Dog(); a.eat(); } }Output
at the age of 10 it should eat only normal foodPrivate Access Modifier: Private can only be accessed within class only. it is the most restricted Access Modifier.
Example:
using System; public class Student { private int id,age; private string name,course; private int batch; public Student(int id,int age,string name,string course,int batch) { this.id=id; this.age=age; this.name=name; this.course=course; this.batch=batch; } private void display() { Console.WriteLine(id+" "+age+" "+name+" "+course+" "+batch); } } public class StudentDetails { public static void Main (string[] args) { Student s1=new Student(101,23,"Varun Kumar","B. Tech.",2017); s1.display(); } }Output
Compilation error (line 26, col 6): 'Student.display()' is inaccessible due to its protection levelInternal Access Modifier: Internal access modifier enables you to access the variable within the current assembly.
Example:
using System; namespace AccessSpecifiers { public class InternalTest { internal string name = "Shantosh Kumar"; internal void Msg(string msg) { Console.WriteLine("Hello " + msg); } } public class Program { public static void Main(string[] args) { InternalTest internalTest = new InternalTest(); // Accessing internal variable Console.WriteLine("Hello " + internalTest.name); // Accessing internal function internalTest.Msg("Peter Decosta"); } } }Protected Internal access Modifier: The protected internal variable can be accessed inside assembly as well as in the other assembly by inheritance.
Example:
using System; using AccessSpecifiers1; namespace AccessSpecifiers1 { public class InternalTest { protected internal string name = "Shantosh Kumar"; protected internal void Msg(string msg) { Console.WriteLine("Hello " + msg); } } } namespace AccessSpecifier2 { public class Program:InternalTest { public static void Main(string[] args) { InternalTest internalTest = new InternalTest(); // Accessing protected internal variable Console.WriteLine("Hello " + internalTest.name); // Accessing protected internal function internalTest.Msg("Peter Decosta"); } } }Output
Hello Shantosh Kumar Hello Peter Decosta