Access Modifiers In C#

Object-Oriented Programming came up with some new concepts and characteristics. Among them securing the data through different means is also a crucial method. Encapsulating means wrapping up of data into a particular unit so that accessing these data need special ways and principles. That is where the concept of access modifiers or access specifiers comes. In this chapter, you will learn about the different access modifiers and their use in various domains.

What Are Access Modifiers In C#?

Access modifiers are also known as access specifiers that are used to preserve the concept of encapsulation of data by specifically restricting the access of variable data using some particular keywords. These are used to specify a specific variable's accessibility or scope as well as functionality within a C# application.

C# provides programmers with five different types of access specifiers. These are:
  1. Public.
  2. Protected.
  3. Internal.
  4. Protected internal.
  5. Private.

Let us discuss each of them in details:

  1. Public Access Modifier is one of the modifiers that allow programmers to expose all of its member variables and methods from outside the class scope. Members of a class which are declared as public can be accessed from anywhere within the program.
  2. Protected Access Modifier: is associated with a property or a method where class members can be accessed only by classes (derived classes) inherited from the present class (base class).
  3. Internal Access Modifier: is used to restrict the access entirely to classes distinct within the existing project. In other words, members of a class with internal access specifier can be accessed from other classes or methods specified within the project application.
  4. Protected internal Access Modifier: allows any class in hiding the member variables as well as class methods from additional and existing class objects and their associated methods, apart from the child class(es) that are there within the same program or application.
  5. Private Access Modifier: is another access specifier that allows programmers to hide or preserve the member variables and methods from other member functions or class objects. The private members of a class can be accessed by only the functions belonging to the same class.

C# Program to Demonstrate Public, Private and Protected Access Modifiers

Example:
using System;  
namespace AccessModifiers  
{  
    class Testing  
    {  
        private int pi = 3;
        public string name = "Richerd Jones";  
        public void PRINT(string msg)  
        {  
            Console.WriteLine("Hi " + msg);  
        }  
        
        protected void PRINTED(string msg)  
        {  
            Console.WriteLine("Welcome " + msg);  
        }  
    }  
    class Test : Testing
    {  
        static void Main(string[] args)  
        {  
            Testing am = new Testing();  
            Test t = new Test();
            Console.WriteLine(" Hi " + am.name);
            am.PRINT("Richerd");  
            t.PRINTED("Mr. Richerd");  
        }  
    }  
}
Output:
Hi Richerd Jones
Hi Richerd
Welcome Mr. Richerd

C# Program to Demonstrate Protected Internal Access Modifier

Example:
using System;  
namespace AccessSpecifiers  
{  
    class TestClass
    {  
        protected internal string name = "Richerd Jones";  
        protected internal void Print(string msg)  
        {  
            Console.WriteLine("Hey " + msg);  
        }  
    }  
    class Test2  
    {  
        static void Main(string[] args)  
        {  
            TestClass tc = new TestClass();  
            Console.WriteLine(" Welcome " + tc.name);  
            tc.Print("Richerd Jones");  
        }  
    }  
}
Output:
Welcome Richerd Jones
Hey Richerd Jones

Scroll Back to Top