Access Modifiers in C#

Access modifiers are used to specify the level at which each variable, function or object can be accessed within the application. In C#, there are five types of access modifiers which are as follows:

  • public
  • private
  • protected
  • internal
  • protected internal

Public Private

Class or members specified as public can be accessed by other functions and objects anywhere within the application. On the other hand, members defined private can only be accessed within the class in which they are defined. Have a look at this example to understand this concept.

using System;
namespace CSharpTutorials {
    public class Person {
        public int age;
        public string name;
        private int salary;

        public Person(int age, string name, int salary) {
            this.age = age;
            this.name = name;
            this.salary = salary;
        }
    }

    class Tutorial {
        static void Main(string[] args) {

            Person p = new Person(20, "John", 100000);

            Console.WriteLine("The age of person is " + p.age);
            Console.WriteLine("The name of person is " + p.name);

            /* The following code will not executed 
             * because salary is private member of person class
             * 
             * Console.WriteLine("The salary of person is " + p.salary);
             */
            Console.ReadKey();
        }
    }
}

Download the above code

In the above code a class named Person has been created. This class has three member variables: age, name and salary. First two variables age, and name are public while salary is private.

Inside the Tutorial class, Person object ā€˜pā€™ has been created. Since age and name are public member variables of Person class they can be accessed inside Tutorial class and can be printed on the console. However, salary is a private variable, therefore it cannot be accessed inside the Tutorial class via dot operator.

Protected

protected member variables and classes can only be accessed within the classes in which they are defined and inside the child classes. This concept will be explained with the help of inheritance in upcoming chapters.

Internal

internal access modifier is used to make variables and functions accessible within the assembly of the class in which they are defined. Have a look at the following example:

using System;
namespace CSharpTutorials {
    public class Person {
        internal int age;
        internal string name;
        internal int salary;

        public Person(int age, string name, int salary) {
            this.age = age;
            this.name = name;
            this.salary = salary;
        }
    }

    class Tutorial {
        static void Main(string[] args) {
            Person p = new Person(20, "John", 100000);

            Console.WriteLine("The age of person is " + p.age);
            Console.WriteLine("The name of person is " + p.name);

            Console.WriteLine("The salary of person is " + p.salary);
            Console.ReadKey();
        }
    }
}

Download the above code

In the above code Person class has three member variables age, name and salary. All of these three variables are accessible within the Tutorial class because both Person and Tutorial classes are within the same assembly.

Protected Internal

protected internal access modifier is a combination of protected and internal modifier. It makes a variable accessible to the child classes that are inside the same assembly of the class in which they were actually defined.

<<< C# Decision statementsLoops in C# >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .