What is the difference between Public, Private, Protected and Internal?

What is the difference between Public, Private, Protected and Internal?

What is the difference between Public, Private, Protected and Internal?

What is the difference between Public, Private, Protected and Internal?: There are five types of access specifiers in c# public, private, protected, internal and protected internal. In this article, I have explained each access specifier with an example.

1) Public

– No restrictions to access.

– The type or member can be accessed by any other code in the same assembly or another assembly that references it.

– Most common access specifier in C#.

using System;
namespace AccessModifiers
{
    class Program
    {
        class sample
        {
            public int num1;
        }
        static void Main(string[] args)
        {
            sample obj = new sample();


            obj.num1 = 10;

            Console.WriteLine("Value of number 1 : {0}", obj.num1);
            Console.ReadLine();
        }
    }
}

From above example you can see num1 can directly accessible by sample object.

2) Private

– The type or member can be accessed only by code in the same class or struct.

– Access is limited to within the class definition and any class that inherits from the class.

using System;
namespace AccessModifiers
{
    class Program
    {
        class sample
        {
            public int num1;
            int num2;
        }
        static void Main(string[] args)
        {
            sample obj = new sample();
            obj.num1 = 100;
             //As num2 is a private variable, It is not accesible by object of sample class
            Console.WriteLine("Value of number 1 : {0}", obj.num1);
            Console.ReadLine();
        }
    }
}
As num2 is a private variable, It is not accessible by object of sample class

3) Protected

– The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.

using System;

namespace Protected_Access_Specifier
{
    class BaseClass
    {
       protected int num1;
        protected int num2;
    }

    class Sample : BaseClass
    {
        static void Main()
        {
            Sample obj1 = new Sample();
            // Direct access to protected members:      
            obj1.num1 = 5;
            obj1.num2 = 10;

            Console.WriteLine("Num1 = {0}, Num2 = {1}", obj1.num1, obj1.num2);
        }
    }
}
If we define variable as a private variable then compile time error will occur.

4) Internal

– The type or member can be accessed by any code in the same assembly, but not from another assembly.

– It is the default access specifiers for a class in C# programming.

using System; 
namespace Internal_Access_Specifier
{
    class Sample
    {
        internal string companyname; // String Variable declared as internal
        public void print()
        {
            Console.WriteLine("\nCompany name is " + companyname);
        }
    } 
    class Program
    {
        static void Main(string[] args)
        {
            Sample obj1 = new Sample();
            Console.Write("Your Company name is :");
            // Accepting value in internal variable
            obj1.companyname = Console.ReadLine();
            obj1.print();
            Console.ReadLine();
        }
    }
}

 

Add a Comment

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