C# Access Modifiers/Specifiers

C# Provides five types of access specifiers:

They are: Public, Private, Protected, Internal, Protected Internal

Public – It specifies that access is not restricted
Protected- It specifies that access is limited to the containing  class or in a derived class
Internal – It specifies that access is limited to the current assembly
Protected Internal – It specifes that access is limited to the current asembly or types from the derived class
Private – It specifies that acess is limited to the containing type

this video will surely help to understand:

Now lets see the example:
public:

using System;

namespace Public
{
    class Program
    {
        public string name = "Shantosh Kumar";

        public void Msge(string msg)
        {
            Console.WriteLine("Hello "+msg);
        } 
    }

    class TestingPublic
    {

        static void Main(string[] args)
        {
            Program p1=new Program();
            Console.WriteLine("Hello "+p1.name);
            p1.Msge("Live");
            Console.ReadKey();
        }
    }
}

protected:

using System;


namespace Protected
{
    class ProtectedTest
    {
        protected string name = "Syed Ahmed Zaki";
        protected void msg(string msg)
        {
            Console.WriteLine("Hello "+msg);
        }
    }
    class Program
    {
        ProtectedTest p1=new ProtectedTest();
        static void Main(string[] args)
        {
            Console.WriteLine("Hello: "+p1.name);
            ProtectedTest.msg("Rahim");
        }
    }
}

output: it will show error

but we can show the output through some tricks

//we are inheriting here the protected class
using System;


namespace Protected
{
    class ProtectedTest
    {
        protected string name = "Syed Ahmed Zaki";
        protected void msg(string msg)
        {
            Console.WriteLine("Hello "+msg);
        }
    }
    class Program:ProtectedTest
    {
        //ProtectedTest p1=new ProtectedTest();
       
        static void Main(string[] args)
        {
            Program pp = new Program();
            Console.WriteLine("Hello "+pp.name);
            pp.msg("Rahim");
            Console.ReadKey();
        }
    }
}

output:

Hello Syed Ahmed Zaki
Hello Rahim

Internal:

using System;

namespace InternalTest
{
   static class InternalTestaaa
    {
        internal static string name = "Zaki Live";

        internal static void msg(string msg)
        {
            Console.WriteLine("Hello: "+msg);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello "+InternalTestaaa.name);
            InternalTestaaa.msg("Chiki Tita");
            Console.ReadKey();
        }
    }
}

Protected Internal:

private:

eg 1:

//different class it is showing error while private

using System;

namespace Private
{
    class PrivateTest
    {
        private string name = "Shantosh Kumar";

        private void Msg(string msg)
        {
            Console.WriteLine("Hello"+msg);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            PrivateTest pvt1=new PrivateTest();
            //Accessing private variable
            Console.WriteLine("Hello "+pvt1.name);
            //accessing private function
            pvt1.Msg();
            Console.ReadKey();
        }
    }

}

eg 2:

 

 

 

 

 

 

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *