Monday, May 10, 2021

C# Access Modifiers

  1. Members of any kind of modifiers are accessible inside its own class.
  2. Members with private modifier are accessible only inside its own class. It means that if you create an instance/object objA of the class A in another class B in the same project, the instance/object objA can access only public and internal members of the base class A. The protected and private members of class A will not be accessible to objA.
  3. Members with protected modifier are accessible only inside its derived classes by the objects of the derived class. if you create an object of base class in the derived class, this object cannot access the protected members of the base class. It means that a protected member of a class is accessible only by the objects of its derived class.
  4. Members with internal modifier are accessible throughout the project by any class. The classes of other projects cannot access the internal members of a class.
  5. Members with public modifier are accessible universally by any class of any project.
Private: The private members of a class can be accessed only inside the class in which the members are declared.
Protected: The protected members of a class can be accessed only inside the derived class of the class by the objects of the derived classes of the class.
Public: The public members of a class can be accessed by the objects of any class.

Fact 1: Members of any kind of modifiers are accessible inside its own class. 

using System;

namespace ConsoleAccessModifiers

{

    class Baseclass

    {

        int NumDefault; //accessible inside this class only

        private int NumPrivate; //accessible inside this class only

        protected int NumProtected; //accessible everywhere inside its derived class

        internal int NumInternal; //accessible everywhere inside this project

        public int NumPublic; //accessible everywhere

 

        public void Show()

        {

            Console.WriteLine("Inside base class:");

            NumDefault = 1; // or this.NumDefault = 1;

            NumPrivate = 2;

            NumProtected = 3;

            NumInternal = 4;

            NumPublic = 5;

            //all kind of modifiers are accessible in its own class

            Console.WriteLine(NumDefault);

            Console.WriteLine(NumPrivate);

            Console.WriteLine(NumProtected);

            Console.WriteLine(NumInternal);

            Console.WriteLine(NumPublic);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Baseclass mybase = new Baseclass();

            mybase.Show();

            Console.ReadKey();

        }

    }

}

Fact 2: Members with private modifier are accessible only inside its own class. It means that if you create an instance/object objA of the class A in another class B in the same project, the instance/object objA can access only public and internal members of the base class A. The protected and private members of class A will not be accessible to objA.

Fact 3: Members with protected modifier are accessible only inside its derived classes by the objects of the derived class. if you create an object of base class in the derived class, this object cannot access the protected members of the base class. It means that a protected member of a class is accessible only by the objects of its derived class. 

using System;

namespace ConsoleAccessModifiers

{

    class First

    {

        int NumDefault; //accessible inside this class only

        private int NumPrivate; //accessible inside this class only

        protected int NumProtected; //accessible everywhere inside its derived class

        internal int NumInternal; //accessible everywhere inside this project

        public int NumPublic; //accessible everywhere

 

        public void Show()

        {

            Console.WriteLine("Inside base class:");

            NumDefault = 1; // or this.NumDefault = 1;

            NumPrivate = 2;

            NumProtected = 3;

            NumInternal = 4;

            NumPublic = 5;

            //all kind of modifiers are accessible in its own class

            Console.WriteLine(NumDefault);

            Console.WriteLine(NumPrivate);

            Console.WriteLine(NumProtected);

            Console.WriteLine(NumInternal);

            Console.WriteLine(NumPublic);

        }

    }

    class Derived : First

    {

        public static void TestProtected()

        {

            Derived d = new Derived();

            Console.WriteLine(d.NumProtected=100);//child class object accessed protected member of base class

            First f = new First();

            // f.NumProtected is illegal, protected member is not accessible inside derived class, if not accessed by the object of child class

            //Console.WriteLine(f.NumProtected);//illegal

            Console.WriteLine(f.NumInternal=222);//OK

            Console.WriteLine(f.NumPublic=300);//OK

        }

       

    }

    class Program

    {

        static void Main(string[] args)

        {

            Derived.TestProtected();

            Console.ReadKey();

        }

    }

}

Fact 4: Members with internal modifier are accessible throughout the project by any class. The classes of other projects cannot access the internal members of a class.

Fact 5: Members with public modifier are accessible universally by any class of any project.





No comments:

Post a Comment

Hot Topics