Home > DeveloperSection > Blogs > Basic Concept for Access Specifier

Basic Concept for Access Specifier


C# C# 
Ratings:
0 Comment(s)
 150  View(s)
Rate this:

Basic Concept for Access Specifier

 

For accessibility in the program we need access specifier which controls whether they can be used from other code in your assembly or other assemblies.


Following are the access specifier in c# for accessibility:


Public:

The program which contain public specifier for function or class or datatype, it can be used anywhere within the program or in other assemblies also.


Private:

The program which contain private specifier for function or class or datatype, it can be used only for that class or function. It is not accessed by other class or assemblies.


Protected:

The program which contain protected specifier for function or class or datatype, it can be used only for that class or function. It is not accessed by other class or assemblies. But It can be used in the derived class.

 

Internal:

The program which contain internal specifier for function or class or datatype, it can be used anywhere within the program. It is not accessed by other assemblies.

 

Protected internal:

The program which contain Protected Internal specifier for function or class or datatype, it can be used anywhere within the program and any derived classes in the other assembly.


For example:

Program for Public Specifier:

class access

    {

       public string Str;

       public void print()

        {

            Console.WriteLine(" string is " + Str);

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            access sp = new access();

           Console.Write("Enter the string:");

            sp.Str = Console.ReadLine();

            sp.print(); 

            Console.ReadLine();

        }

    }

}

Program for Private Specifier

class access

   {

        private string Str;

        public void print()

        {

            Console.WriteLine(" string is " + Str);

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            access sp = new access();

           Console.Write("Enter the string:");

            sp.Str = Console.ReadLine(); // error will occur

            sp.print(); 

            Console.ReadLine();

        }

    }

}

Program for Protected Specifier

class access
    {      

        Protected string Str;

        public void print()

        {

            Console.WriteLine("string is " + Str);

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            access sp = new access();

           Console.Write("Enter the string:");        

            sp.Str = Console.ReadLine(); // error will occur

            sp.print(); 

            Console.ReadLine();

        }

    }


Program for Internal Specifier:

 class access 

    { 

        internal string Str;

        public void print()

        {

            Console.WriteLine("String is " + Str);

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            access sp = new access();

            Console.Write("Enter the string:"); 

            sp.Str = Console.ReadLine();

            sp.print();

            Console.ReadLine();

        }

    }

 

Program for Protected Internal Specifier:

    class access

    {      

        protected internal string Str;

        public void print()

        {

            Console.WriteLine("String is " + Str);

        }

    } 

 

    class Program

    {

        static void Main(string[] args)

        {

            access sp  = new access();

            Console.Write("Enter the string  ");

            sp.Str = Console.ReadLine();

            sp.print();

            Console.ReadLine();

        }

}


Don't want to miss updates? Please click the below button!

Follow MindStick