×

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#:
  1. Private
  2. Protected
  3. Internal
  4. Protected internal
  5. Public
Let’s see each one of them in detail:
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
Public Access modifier: It makes data which is accessible publically. It doesn’t restricts the scope of data.

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. 2017
Protected 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 level
Protected 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 food
Private 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 level
Internal 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
 

Related Topics

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

3 minutes read.

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes read.

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.

C# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 minutes read.

C# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 minutes read.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.

C# Keywords

Keywords are the reserved words with special meaning. They can’t be used as variables and Identifiers. If you want to use them as identifiers, you must use ‘@’ character prior...

1 minute read.