C# Access Modifiers

Access modifiers are used to implement an important aspect of OOP known as Data Hiding.

There are three types of access modifiers in C++

1. Public

2. Private

3. Protected 

4. Internal

Note -> If we do not specify any access modifiers for the members inside the class then by default the access modifier for the member will be Private.

1. Public : The class members which are declared under the public specifier will available to everyone and can be accessed by other classes and functions too.

The public members of a class can be accessed from anywhere in the program using direct member access operator (.) with the object of that class.

//Syntax
public <TypeName>
using system;
namespace AccessModifiers{
    class Student{
        public int rollNo;
        public string name;
        public Student(int r, string n){
            rollNo = r;
            name = n;
        }
        public void getRoll(){
            return rollNo;
        }
        public void getName(){
            return name;
        }
    }
    class Program{
            static void main(string[] args)
            {
                Student s = new Student();
                //Student's roll no which is in public in Student is shown.
                Console.WriteLine("Student Roll No is : ",s.rollNo);
                //Student name is shown by a method which has public access modifier.
                Console.WriteLine("Student Name is : ",s.getName());
            }
        }
}

n the above program the data member radius is declared as public so it could be accessed outside the class and thus was allowed access from inside main().

2. Private :  The class member declared as private can be accessed only by the member functions inside the class. They are not allowed to be accessed  directly by any object or function outside the class.

Only the member functions or the friend functions are allowed to access the private data members of a class.

//Syntax
private <TypeName>
using system;
namespace AccessModifiers{
    class Test{
        private int value;
        public Test(int val){
            value = val;
        }
        public void getValue(){
            return value;
        }
    }
    class Program{
            static void main(string[] args)
            {
                Test t = new Test();
                Console.WriteLine("Value is : ",t.value);
                //Value is not shown as value property has private access modifier.
                //Instead it will throw an error.
            }
        }
}

The output of the above program is a compile time error because we are not allowed to access the private data members of a class directly outside the class.

Yet as access to obj.radius is attempted, radius being a private data member we obtain a compilation error.

However, we can access the private data members of a class indirectly using the public member functions of the class.

3. Protected : Protected access modifier is similar to private access modifier in the sense that it can’t be accessed outside of its class unless with the help of friend class, the difference is that the class members declared as protected can be accessed by any subclass(derived class) of that class as well

//Syntax
protected <TypeName>
using system;
namespace AccessModifiers{
    class A{
        protected int value;
        public A(){
            value = 10;
        }
    }
    class B:A{
        public void getValue(){
            return value;
        }
    }
    class Program{
            static void main(string[] args)
            {
                A obj1 = new A();
                B obj2 = new B();
                //As value has protected mode it can be accessed by class B.
                Console.WriteLine("Value is : ",obj2.getValue());
            }
        }
}

4. Internal Accessibility mode : Internal Accessibility mode makes the access available to anywhere inside the same namespace.

//Syntax
internal <TypeName>
using system;
namespace AccessModifiers{
    internal class A{
        int value;
        public A(){
            value = 10;
        }
    }
    class Program{
            static void main(string[] args)
            {
                A obj1 = new A();
                //As class has internal access modifier it can be accessed anywhere inside same namespace.
                Console.WriteLine("Value is : ",obj1.getValue());
            }
        }
}