×

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# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 minutes read.

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.

C# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

1 minute read.

C# static field

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only...

5 minutes read.

C# Namespace

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace...

1 minute read.

C# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes read.

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 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# 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# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

3 minutes read.

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

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

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using...

3 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes 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# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.