Access modifiers in C#

tkarropoulos

Theodore Karropoulos

Posted on May 20, 2021

Access modifiers in C#

Access modifiers are basically keywords that set the access or level of visibility on classes, methods, variables in our code. In other words if a class, method, variable can be used or be accessed from code of its assembly or other assemblies.

Type of access modifiers

There are currently six base types of access modifiers in C#.

  1. public
  2. private
  3. protected
  4. internal
  5. protected internal
  6. private protected

The public access modifier

Probably the most famous one. When we declare a class as public then it can be accessed from any class.

public class PublicClass
{
    // Some awesome staff here...
}
Enter fullscreen mode Exit fullscreen mode

The private access modifier

Properties or methods that are declared as private can be accessed only within the specific class or struct

public class PublicClass
{
    private void string PrivateMethod()
    {
        // do great things here
    }    
}
Enter fullscreen mode Exit fullscreen mode

The protected access modifier

Properties or methods that are declared as protected can be accessed only by code in the same class or in a class that is derived from that class.

public class ParentClass
{
    protected int _protectedInt;
}

public class DerivedClass: ParentClass
{
    static void Main()
    {
        var derivedClass = new DerivedClass();

        // Direct access to protected members.
        derivedClass._protectedInt = 10;
    }
}
Enter fullscreen mode Exit fullscreen mode

The internal access modifier

Types or members can be accessed from anywhere inside the same assembly but not from other assemblies

internal class InternalClass
{
    // Our hello world code here
}
Enter fullscreen mode Exit fullscreen mode

The protected internal access modifier

A protected internal member is accessible from the current assembly or from types that are derived from the containing class.

public class AssemblyOneBaseClass
{
   protected internal int someInt = 0;
}

public class AssemblyOneTestClass
{
    void Access()
    {
        var assemblyOneBaseClass = new AssemblyOneBaseClass();
        assemblyOneBaseClass.someInt = 5;
    }
}


public class AssemblyTwoDerivedClass : AssemblyOneBaseClass
{
    static void Main()
    {
        var assemblyOneBaseClass = new AssemblyOneBaseClass();
        var assemblyTwoDerivedClass = new AssemblyTwoDerivedClass();

        assemblyOneBaseClass.someInt = 10;
    }
}
Enter fullscreen mode Exit fullscreen mode

The private protected access modifier

A private protected member is accessible by types derived from the containing class, but only within its containing assembly.

public class BaseClass
{
    private protected int someInt = 0;
}

public class DerivedClass : BaseClass
{
    void Access()
    {
        var baseClass = new BaseClass();
        someInt = 5;
    }
}
Enter fullscreen mode Exit fullscreen mode

All the above in a glance
image
If you would like to read more about access modifiers in C# please visit this link , or check out my blog for more stories.

💖 💪 🙅 🚩
tkarropoulos
Theodore Karropoulos

Posted on May 20, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Fundamentals of TDD in C#
csharp Fundamentals of TDD in C#

September 18, 2021

C# Channels
csharp C# Channels

July 31, 2021

Access modifiers in C#
csharp Access modifiers in C#

May 20, 2021

Hash table efficiency example
csharp Hash table efficiency example

October 3, 2020