C# Access Modifiers / Specifiers (With Examples)

Access Modifiers in C#
C# Access Modifiers

Introduction: Access Modifiers

In C#, access modifiers are keywords that describe how a class, its properties, fields, or methods are accessible in a program. These access modifiers are mostly used for defining the scopes of types, and members, and to prevent external programs from accidentally changing data.

 

What are the types of access modifiers in C#?

The following six access modifiers are available in C#.

  • public
  • protected
  • internal
  • protected internal
  • private
  • private protected

Access Modifiers In C# – Summary Table

You can quickly understand all of the access modifiers by using the diagram below.

C#-Access Modifiers diagram
C#-Access Modifiers diagram

The following access modifiers are available in C#:

No.C# ModifiersDescription
01.publicCode is accessible from all classes. This means that other code in the same assembly or another assembly that references it can access the type or member.
02.privateThe code is only accessible within the same class or struct.
03.protectedThe code (type or member) is accessible within the same class or in a class inherited from that class.
04.internalThe code (type or member) can be accessed by any code in the same assembly, but not by code from another assembly.
05.protected internalThe type or member can be accessed by any code in the assembly in which it is declared, or by a derived class in another assembly.
06.private protectedThe code  (type or member) can be accessed by types derived from the class that is declared within the current assembly. In other words, access is granted to the contained class and its derived types in the current assembly.
c# access modifiers/specifiers

Public Access Modifier In C#

The  public  keyword makes classes (including nested classes), properties, methods, or fields accessible from anywhere, both inside and outside of the program assembly.

Accessibility:

  • A type or member declared as public can be accessed by any other code in the same assembly or another assembly that references it.
  • All classes can access the code.
  • The public members can be accessed from any part of the code. As a result, they are the least safe. Any code logic can change the value, resulting in unanticipated behavior. As a result, extreme caution must be exercised before making any object public.

Example:

The following examples demonstrate how to specify public access modifiers on a class and member.

using System;

namespace publicAccessModifierExample
{
    // public access modifier example

    public class Person
    {
        // Declaring member name as public
        public string name = "Shekh Ali";
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Creating object of the class Person
            Person person = new Person();

            // Displaying name of the person
            Console.WriteLine($"Name: {person.name}");
            Console.WriteLine();
        }
    }

}
// Output:
// Name: Shekh Ali

In the above program, the name variable implements the public access modifier and can be accessed from anywhere in the project. Therefore, there are no accessibility restrictions.

Protected Access Modifier In C#

The  protected  access modifier indicates that the members can be accessed in both the containing class and derived classes.

Accessibility:

  • Protected members are only visible to their own classes and their derived classes.
  • Protected members can’t be accessed by the object of the class.

Example:

The following examples demonstrate how to specify protected access modifiers on a type member.

using System;
namespace protectedAccessModifierExample
{
    class BaseClass
    { 
       // protected variable
       protected int number; 
       // constructor
       public BaseClass()
        {
            // protected variable can be accessed inside this 'BaseClass' class
            number = 50;
        }
    }
    // 'DerivedClass' inherits the 'BaseClass'
    class DerivedClass : BaseClass 
    {
        // Members of class 'DerivedClass' can access 'number'
        public void Display()
        {         
            // we can access 'number' in this class as well
            // because it derives from the NumberClass class
            Console.WriteLine($"number = {number}"); 
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Creating instance of the base class
            BaseClass baseClass = new BaseClass();

          // Error. The 'BaseClass.number' variable is inaccessible due to its protection level. 
            // The Program class doesn't derive from the NumberClass
            // Console.WriteLine(baseClass.number); 

            // Creating instance of the derived class
            DerivedClass derivedClass = new DerivedClass();
            // Displaying the value of 'number'
            derivedClass.Display();

            Console.WriteLine();           
        }
    }
}
// Output:
// number = 50

In the above code, the class ‘DerivedClass’ inherits from ‘BaseClass’, therefore, any protected members of ‘BaseClass’ can be accessed from ‘DerivedClass’.

Internal Access Modifier In C#

In C#, the  internal  access modifier specifies that the type or members declared as internal can only be accessed in its own assembly, but not in other assemblies.

  • Internal access modifier is commonly used in component-based development because it allows a set of components to work together in a private manner without exposing them to the rest of the application code.
  • Access is limited to the current assembly.

Example:

This example contains two files in two separate projects Assembly1.cs and Assembly2.cs.  The first file contains an internal base class BaseClass. In the second file, an attempt to instantiate BaseClass will fail.

namespace Assembly1
{ 
    // Assembly1.cs
    internal class BaseClass
    {
        internal static int number = 50;
    }
}

Assembly2.cs

using Assembly1;
namespace Assembly2
{
    // Assembly2.cs
    class Program
    {
        static void Main(string[] args)
        {
            //  instantiate BaseClass
            BaseClass baseClass = new BaseClass();  
            // error: Base class is inaccessible due to its protection level                        
        }
    }
}

internal access modifier in C#
image: Internal access modifier in C#

Protected Internal Access Modifier In C#

The  protected internal  access modifier in C# is a combination of the protected and internal access modifiers. Therefore, protected internal types or members can only be accessed inside the same assembly or through derived classes in other assemblies.

Consider the following code snippet as an example:

Example:

using System;
namespace Assembly1
{ 
    // Assembly1.cs
    public class BaseClass
    { 
        // Declare protected internal member
        protected internal int number = 50;
    }
    class TestAccessInSameAssembly
    {
        void Access()
        {
            BaseClass baseObject = new BaseClass();
            // Accessible in the same assembly
            Console.WriteLine(baseObject.number);
        }
    }
}

Let’s write code in the Assembly2.cs file

using Assembly1; // referencing the first assembly
using System;
namespace Assembly2
{
    // Assembly2.cs
    class Program : BaseClass
    {
        static void Main(string[] args)
        {
            //  instantiate BaseClass
            BaseClass baseClass = new BaseClass();
            //baseClass.number;
            // Error CS1540, because 'number' can only be accessed by
            // classes derived from BaseClass.

            // Creating instance of the derived class
            Program program = new Program();
            // 'number' is accessible in the derived class
            Console.WriteLine($"number = {program.number}");

        }
    }
}

protected internal access modifier output
image-protected internal access modifier output

Private Access Modifier In C#

The members having the  private  access modifier, such as variables and methods, are only accessible within the body of the class or struct in which they are declared.

Accessibility:

  • The private keyword is just a member access modifier and cannot be explicitly used by classes or structures.
  • Nested types in the same body can also access these private members.
  • When a private member is referenced outside of the class or structure in which it is declared, a compile-time error will be thrown.

Example:

Let’s take an example and see what happens when we use the private access modifier.

using System;
namespace PrivateAccessModifier
{ 
    class PrivateClass
    {  // private variable
        private int number = 50;

        public double PrintNumber
        {  
            // private members are accessible inside the same class
            get { return number; }
        }

    }    
    class Program 
    {
        static void Main(string[] args)
        {
            PrivateClass privateClass = new PrivateClass();

            // Compile Error - 'privateClass.number' is inaccessible due to its protection level
            // Console.WriteLine($"number = {privateClass.number}");

            // 'number' is indirectly accessed via public property
            Console.WriteLine($"number = {privateClass.PrintNumber}");
        }
    }
}

Private Protected Access Modifier In C#

The  private protected  modifier is available in C# version 7.2 and later. It’s used to specify that the containing class and its derived types in the current assembly can only access private protected members.

  • When we wish to restrict object access even in derived classes in other assemblies, we can use the private protected modifier.
  • The members of the struct cannot be private protected because the structure cannot be inherited.

Example:

Consider the following code snippet as an example:

using System;
namespace PrivateProtectedAccessModifier
{
    class BaseClass
    {
        // private protected member
        private protected int number = 50;

        // number is Accessible only inside the current class 
        // in the current assembly
        public int getNumber()
        {
            return number;
        }

    }
    class DerivedClass : BaseClass
    {
       
        static void Main(string[] args)
        {
            BaseClass baseClass = new BaseClass();
            // baseClass.number = 10;
            // Compile-time Error CS1540, because 'number' can only be accessed by
            // classes derived from BaseClass.
         

            // OK, accessed value of 'number' through the current derived class instance
            DerivedClass derivedClass = new DerivedClass();       
            Console.WriteLine($"number = {derivedClass.number}");
        }
    }
}

// Output:
// number = 50

Learn more about this on MSDN.

FAQs

Q: What level of accessibility do members of an interface have?

Ans: Members of the interface are by default public. You are not permitted to use any explicit access modifiers with interface members.

Q: Is it possible for derived classes to have a higher level of accessibility than their base types?

Ans: No, Derived classes cannot have greater accessibility than their base types. You are not allowed to declare a public class B that derives from an internal class A.

Q: Is it possible to make struct members as protected in C#?

Ans: No, struct members cannot be declared as protected. This is because the structure does not support inheritance.

Q: What is the default access modifier for a class, struct, or interface declared within a namespace?

Ans: Internal is the default access modifier for class, struct, and interface.

Q: What is the default access modifier for class members?

Ans: If we do not specify an access modifier, all class members are private by default.

Q: Why is the access modifier not allowed in namespaces?

Ans: Namespaces do not support access modifiers because they do not have access restrictions.

Q: What is the importance of access modifiers in C#?

Ans: Access modifiers prevent unauthorized data alteration by other classes or programs.

References: MSDN- C# Access Modifiers, W3Schools- access modifiers in c#

I hope this post helped you learn more about “access modifiers in C#”. If you like this post, please share it with others. I welcome your comments, suggestions, and constructive criticism. Thanks.

Recommended Articles:

Leave a Comment