Tuesday, 12 April 2016

What are access modifiers in C#?


Access modifiers determine the scope of the method or variables that can be accessed from other various objects or classes. There are 5 types of access modifiers, and they are as follows:-
  • Public. 
  • Private. 
  • Protected. 
  • Internal 
  • Protected Internal 
Public: The class member, that is defined as public can be accessed by other class member that is initialized outside the class. A public member can be accessed from anywhere even outside the namespace.

Example:-

using System;
namespace Public_Access_Specifiers
{
    class access
    {
        // String Variable declared as public
        public string name;

        // Public method
        public void print()
        {
            Console.WriteLine("\nMy name is " + name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            access ac = new access();
            Console.Write("Enter your name:\t");

            // Accepting value in public variable that is outside the class
            ac.name = Console.ReadLine();
            ac.print();
            Console.ReadLine();
        }
    }
}

Output:



Private: The private access specifiers restrict the member variable or function to be called outside from the parent class. A private function or variable cannot be called outside from the same class. It hides its member variable and method from other class and methods. However, you can store or retrieve value from private access modifiers using get set property. You will learn more about get set property in lateral chapter.

Example:

using System;
namespace Private_Access_Specifiers
{
    class access
    {
        // String Variable declared as private
        private string name;

        public void print() // public method
        {
            Console.WriteLine("\nMy name is " + name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            access ac = new access();
            Console.Write("Enter your name:\t");

            // raise error because of its protection level
            ac.name = Console.ReadLine();

            ac.print();
            Console.ReadLine();
        }
    }
}


Output:

Error 1: Private_Access_Specifiers.access.name' is inaccessible due to its protection level

In the above example, you cannot call name variable outside the class because it is declared as private.

Protected: The protected access specifier hides its member variables and functions from other classes and objects. This type of variable or function can only be accessed in child class. It becomes very important while implementing inheritance.

Example:


using System;
namespace Protected_Specifier
{
    class access
    {
        // String Variable declared as protected
        protected string name;
        public void print()
        {
            Console.WriteLine("\nMy name is " + name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            access ac = new access();
            Console.Write("Enter your name:\t");
            // raise error because of its protection level
            ac.name = Console.ReadLine();
            ac.print();
            Console.ReadLine();
        }
    }
}

Output: 
'Protected_Specifier.access.name' is inaccessible due to its protection level.

This is because; the protected member can only be accessed within its child class. You can use protected access specifiers as follow:

Example:

using System;
namespace Protected_Specifier
{
    class access
    {
        // String Variable declared as protected
        protected string name;
        public void print()
        {
            Console.WriteLine("\nMy name is " + name);
        }
    }

    class Program : access // Inherit access class
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.Write("Enter your name: ");
            p.name = Console.ReadLine(); // No Error!!
            p.print();
            Console.ReadLine();
        }
    }
}

Output:



Internal: The internal access specifier hides its member variables and methods from other classes and objects, that is resides in other namespace. The variable or classes that are declared with internal can be access by any member within application. It is the default access specifiers for a class in C# programming.

Example:

using System;
namespace Internal_Access_Specifier
{
    class access
    {
        // String Variable declared as internal
        internal string name;
        public void print()
        {
            Console.WriteLine("\nMy name is " + name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            access ac = new access();
            Console.Write("Enter your name:\t");
            // Accepting value in internal variable
            ac.name = Console.ReadLine();
            ac.print();
            Console.ReadLine();
        }
    }
}

Output :



Protected Internal: The protected internal access specifier allows its members to be accessed in derived class, containing class or classes within same application. However, this access specifier rarely used in C# programming but it becomes important while implementing inheritance.

Example:

using System;
namespace Protected_Internal
{
    class access
    {
        // String Variable declared as protected internal
        protected internal string name;
        public void print()
        {
            Console.WriteLine("\nMy name is " + name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            access ac = new access();
            Console.Write("Enter your name:\t");
            // Accepting value in protected internal variable
            ac.name = Console.ReadLine();
            ac.print();
            Console.ReadLine();
        }
    }
}


No comments:

Post a Comment

Featured post

What is SharePoint?

Microsoft SharePoint is an extensible platform that provides a range of products that can help organizations with solution for a variety...