Access Modifiers in C#

Understanding Access Modifiers in C#

Access modifiers define the accessibility of objects and all of their members, all objects and members have access modifiers implemented, even if it's not stated the default access modifier is applied then.


Understanding Access Modifiers in C#- DotNetKida



Access Modifiers Types


There are four access modifier keywords and combinations of access modifier keywords that we can apply to an object or member such as a field or method.

Private Access Modifier : 

Using the private keyword we apply a private access modifier to an object, those Objects are accessible only inside a structure or a class. As result, it can’t be accessed outside the class/ structure:

Example:


class PersonDetails
{
  //Using private keyword
  private string name = "DotNetKida";
}
class Program
{
  static void Main(string[] args)
  {
    PersonDetails details = new PersonDetails();
    Console.WriteLine(details.name);
    //Error
   // ' PersonDetails.name' is inaccessible due to
   // its protection/ accessibility  level
   }
}


Internal Access Modifier:

Using an internal keyword we apply an internal access modifier to an object, those Objects are accessible only inside their own assembly but can not be accessed in other assemblies:

Example:


       //First Project ASSEMBLY
     public class DetailClassInFirstAssembly
     {
      internal string name = "DotNetKida"; //we can access this variable inside this class
      }
     
      class ProgramInFirstAssembly
      {
         void Print()
         {
           DetailClassInFirstAssembly obj = new DetailClassInFirstAssembly();
           Console.WriteLine(obj.name); // can be access anywhere in this project
          }
       }

        //Second project ASSEMBLY
        class Program
        {
          void Print()
          {
           DetailClassInFirstAssembly obj2 = new DetailClassInFirstAssembly();
//The Program class in second project can't access the internal members from another project
           Console.WriteLine(obj2.name); // Error. The name variable can't be access due to its protection level.
                                             
          }
        }


Protected Access Modifier:

Using the protected keyword we apply a protected access modifier to an object,
protected keyword implies that the object is only accessible inside the class and in all classes that derive from that class, we will talk in more detail about Inheritance in another article.

Example:


class NameClass
        {
            //we can access this variable inside this class
            protected string name = "DotNetKida";
        }

        //this is inheritance. ChildClass derives from the NameClass class
        class ChildClass : NameClass
        {
            void Print()
            {
     //we can access it in this class as well because it derives from the NameClass class
                Console.WriteLine(name);
            }
        }
        class Program
        {
            void Print()
            {
                NameClass objNameClass = new NameClass();
                // Error. The name variable is inaccessible due to its protection level.
                Console.WriteLine(objNameClass.name);
                // The Program class doesn't derive from the NameClass
            }
        }


Public Access Modifier:

One of the simplest ones is the objects that are decorated with the public keyword they can be accessed from everywhere with no limitations. That's why there is no accessibility restriction:

Example:
Let's take the last example from protected if we change it protected to the public let's see what happened:


class NameClass
        {
            //we can access this variable inside this class
            public string name = "DotNetKida";
        }

        //this is inheritance. ChildClass derives from the NameClass class
        class ChildClass : NameClass
        {
            void Print()
            {
     //we can access it in this class as well because it derives from the NameClass class
                Console.WriteLine(name);
            }
        }
        class Program
        {
            void Print()
            {
                NameClass objNameClass = new NameClass();
//Now the variable can be access because it is set to public
                Console.WriteLine(objNameClass.name);
            }
        }


Protected Internal Access Modifier:

Using the protected internal keyword we apply a protected internal access modifier to an object,
the protected internal access modifier is a combination of internal and protected. By using this, it can be accessed (protected internal member) only in the same (project)assembly or in a derived class in other assemblies (projects):

Example:



//First Project (ASSEMBLY)
        public class NameClassInFirstProject
        {
            //we can access this variable inside this class
            protected internal string name = "DotNetKida";
        }
        class ProgramInFirstProject
        {
            void Print()
            {
                NameClassInFirstProject obj = new NameClassInFirstProject();
      // This is OK. Anywhere in this project (assembly) we can access the name variable.
                Console.WriteLine(obj.name);
            }
        }
        //Second project (ASSEMBLY)
        class Program : NameClassInFirstProject //Inheritance
        {
            void Print()
            {
     //This is OK as well. The class Program derives from the NameClassInFirstProject .
                Console.WriteLine(name);
            }
        }


Private Protected Access Modifier:

Using the private protected keywords we apply the private protected access modifier to an object.
The private protected access modifier is a combination of private and protected keywords. We can access members inside the containing class or in a class that derives from a containing class, but only in the same assembly. If we try to access it from another assembly, we will get an error.
Example:

// Assembly1.cs 
       public class ParentClass
    {
        private protected int myNewValue = 0;
    }

    public class ChildClassParentClass
    {
        void Access()
        {
            var baseObject = new ParentClass();

            // Error CS1540, because myNewValue can only be accessed by
            // classes derived from ParentClass.
            // baseObject.myNewValue = 5; 

            // OK, accessed through the current derived class instance
            myNewValue = 5;
        }
    }

    // Assembly2.cs 
    
    class ChildClass2ParentClass
    {
        void Access()
        {
            // Error CS0122, because myNewValue can only be
            // accessed by types in Assembly1
            // myNewValue = 10;
        }
    }



Conclusion


I hope it will help you to understand access modifiers in C#. Now we have learned what types of access modifiers can be used in C# and their limitations.

References :

Post a Comment

0 Comments