Signup/Sign In

C# Access Modifiers

Posted in Programming   LAST UPDATED: SEPTEMBER 22, 2021

    This article introduces the concept of access modifiers in C# programming language. As we know, Abstraction, Encapsulation, Inheritance, and Polymorphism are the four pillars of object-oriented programming. Encapsulation is achieved by using the access modifiers. The access modifiers gives us an ability to control the access(who can access what) and protect certain features in the project/application based on the specified modifiers. As you will go through the implementations, you will come to know the access modifiers and how can we use it to achieve encapsulation.

    Introduction to C# Access Modifiers:

    The access modifiers are the keywords using which we can specify the access for a member of class, whether it can be accessed by some other class or not, whether it can be accessed by the child class or not and so on. There are four access modifiers plus two more combinations, and they are as follows:

    1. public: using public, we can access any members in the project without restrictions

    2. private: using private, members are accessible only inside a class or a structure. This is the default access modifier if the modifier not mentioned explicitly

    3. protected: using protected, we can access to the containing class or types derived from the containing class

    4. internal: using internal, we can access throughout the assembly

    5. protected internal: using protected internal, we can access to the current assembly or types derived from the containing class

    6. private protected: using private protected, within the current assembly, we can access to the containing class or types derived from the containing class

    Let's have some practical example for each of the access modifier:


    1. C# public Access Modifier:

    The public access modifier when used with a class member(variable or method) makes it accessible for everyone without restriction. A public member variable(property) or method of a class can be accessed in some other class wihout restriction. The below example shows how to use and access the public members anywhere in the project/program.

    Filename: Program.cs

    using System;
    
    namespace Studytonight
    {
        class NoBank
        {
            public string IFSC = "NOBANK08932";
        }
        public class Program
        {
            public static void Main(string[] args)
            {
                NoBank nb = new NoBank();
                // accessing the public variable IFCS of class NoBank
                Console.WriteLine("NoBank IFSC Code is " + nb.IFSC);
            }
        }
    }

    Output:

    NoBank IFSC Code is NOBANK08932

    In the above example, we are accessing the property IFSC (Indian Financial System Code) for NoBank class by creating the object of NoBank class and accessing the public variable IFSC using the syntax object.variable_name.


    2. C# private Access Modifier:

    Using the private access modifier, members are accessible only inside a class or a structure, and it is the default access modifier if any modifier is not mentioned explicitly. The following example is for a private access modifier.

    Filename: Program.cs

    using System;
    namespace Studytonight
    {
        class NoBank
        {
            private string IFSC = "NOBANK08932";
        }
        class Program
        {
            public static void Main(string[] args)
            {
                NoBank nb = new NoBank();
                Console.WriteLine("NoBank IFSC Code is " + nb.IFSC);
            }
        }
    }

    Output:

    IFSC is inaccessible due to its protection level

    In the above example, we got the compile-time error as we initialized the variable IFSC with a private access modifier and then tried to access it in another class named Program. We can access private data members within the same class only, or we can create public getter and setter methods for accessing the private variables of class outside the class. The following example shows how to access private data members within the same class:

    Filename: Program.cs

    using System;
    
    namespace Studytonight
    {
        class Program  
        {
            private string IFSC = "NOBANK08932";
            public static void Main(string[] args)
            {
                Program p = new Program();
                Console.WriteLine("NoBank IFSC Code is " + p.IFSC);
            }
        }
    }

    Output:

    NoBank IFSC Code is NOBANK08932


    3. C# protected Access Modifier:

    Using the protected access modifier, we can make a member variable or class method accessible only to the child class, which means if you make a class member variable protected, then the class inheriting that class can access the protected members of that class, not other class can access the protected members.

    Filename: Program.cs

    using System;
    
    namespace Studytonight
    {
        class NoBank
        {
            protected string IFSC = "NOBANK08932";
        }
        class Program:NoBank
        {
            public static void Main(string[] args)
            {
                Program p = new Program();
                Console.WriteLine("NoBank IFSC Code is " + p.IFSC);
            }
        }
    }

    Output:

    NoBank IFSC Code is NOBANK08932

    In the above example, we have accessed protected members of the NoBank class in its child/derived class Program. Please refer to our separate article on Inheritance for better understanding. Try removing the inheritance, i.e. use only class Program to define the Program class and you will start getting error.


    4. C# internal Access Modifier:

    When we use the internal access modifier with any class member variable or member function, we can access them only within the same file. Its implementation may look the same as public access modifier, but it is unlike the public modifier as internal is accessible within the same assembly, and the public is accessible anywhere (based on the given references of same or other assemblies). In the following example of an internal access modifier, we have accessed the internal string variable in the Program class by creating an object of the NoBank class.

    Filename: Program.cs

    using System;
    
    namespace Studytonight
    {
        class NoBank
        {
            internal string IFSC = "NOBANK08932";
        }
        class Program
        {
            public static void Main(string[] args)
            {
                NoBank nb = new NoBank();
                Console.WriteLine("NoBank IFSC Code is "+nb.IFSC);
            }
        }
    }

    Output:

    NoBank IFSC Code is NOBANK08932


    5. C# protected internal Access Modifier:

    The protected internal keyword is a combination of protected and internal access modifiers. It gives the ability to access the class members(variables and functions) in the same file or for the types derived from the containing class(i.e. child class can access members of parent class). In the below example, we have initialized a string variable and accessed it into another assembly by providing a reference of the first assembly.

    Filename: Program.cs (First Assembly)

    using System;
    
    namespace Studytonight
    {
        class NoBank
        {
            protected internal string IFSC = "NOBANK08932";
        }
        class Program
        {
            public static void Main(string[] args)
            {
                NoBank nb = new NoBank();
                Console.WriteLine("NoBank IFSC Code is " + nb.IFSC);
            }
        }
    }

    Output:

    NoBank IFSC Code is NOBANK08932

    In the above example, we initialized a string with protected internal access modifier and accessed it in the Program class by creating the object of NoBank class, but the class NoBank can also be derived into another assembly by providing a reference of the first assembly. Following is the second assembly, where we have shown how to give a reference.

    Filename: Program.cs (Second Assembly)

    using System;
    using Reference // give the actual reference here (use solution explorer in the Visual Studio to add the references)  
    
    class Program:NoBank // Inheritance
    {
        public static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine("NoBank IFSC Code is " + p.IFSC);
        }
    }


    6. C# private protected Access Modifier:

    The private protected keyword is a combination of private and protected access modifiers. Using this we canrestrict the access for class members to only within the current assembly (unlike internal protected) to the containing class or to the types derived from the containing class(i.e. its child classes). Please refer to the above example of a private access modifier, where we were not able to access the data members into another class. The same concept is applied in the case of private protected and we won't be able to access the member into another assembly using inheritance. If we try to access it from another assembly, we will get an error.

    Note: The private protected access modifier is valid in C# version 7.2 and later.


    Conclusion:

    We hope this article helped you to understand the concept of access modifiers and how to achieve the encapsulation by using it. If you have any queries regarding this topic, then please let us know in the comment section below. We are happy to solve your doubts.

    You may also like:

    About the author:
    Subject Matter Expert of C# Programming at Studytonight.
    Tags:C#C# TutorialAccess Modifiers
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS