Loading...


There are five access modifier in C#. 
1) Public 
2) Private 
3) Protected 
4) Internal 
5) Protected Internal

1.) Public modifier
The class member, that is defined as a public can be accessed by other class members that are initialized outside the class. A public member can be accessed from anywhere even outside the namespace.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text; 

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();

        }

    }

}


2.) Private modifier

The private access specifiers restrict the member variable or function to be called outside of the parent class. A private function or variable cannot be called outside of the same class. It hides its member variable and method from other class and methods. However, you can store or retrieve the value from private access modifiers using get the set property.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text; 

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();

        }

    }

}


3.) Protected modifier

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.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text; 

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();

        }

    }

}