Loading, please wait...

A to Z Full Forms and Acronyms

Access modifiers in c# with example | C# 11.0

In this post, we'll learn about Access modifiers in c# 11 with an example

What do we mean by Access modifiers?

Access modifiers in C# are keywords used to define the accessibility of classes, methods, fields, and other members within a program. 

There were five access modifiers in C# a long time: 

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

Newly Added access modifiers in C# 11:

  1. private protected
  2. file

Let's see in detail with an example:

1. Public access modifiers:

Members declared as public are accessible from anywhere within the program, as well as from other programs that reference the current program.
Example:

public class ExampleClass
{
    public int exampleField; //public field
    public void ExampleMethod() //public method
    {
        // method implementation
    }
}

2. Private access modifiers:

Members declared as private are only accessible within the same class. They cannot be accessed by any code outside of the class, including derived classes.
Example:

public class ExampleClass
{
    private int exampleField; //private field
    private void ExampleMethod() //private method
    {
        // method implementation
    }
}

3. Protected access modifiers:

Members declared as protected are accessible within the same class, as well as by any derived class. They cannot be accessed by any code outside of the class hierarchy.
Example:

public class ExampleClass
{
    protected int exampleField; //protected field
    protected void ExampleMethod() //protected method
    {
        // method implementation
    }
}

public class DerivedClass : ExampleClass
{
    public void AccessProtected()
    {
        exampleField = 10; //accessing protected field from derived class
        ExampleMethod(); //accessing protected method from derived class
    }
}

4. Internal access modifiers:

Members declared as internal are accessible within the same assembly (a collection of files that are built into a single unit), but not from outside the assembly.
Example:

internal class ExampleClass
{
    internal int exampleField; //internal field
    internal void ExampleMethod() //internal method
    {
        // method implementation
    }
}

5. Protected Internal access modifiers:

Members declared as protected internally are accessible within the same assembly, as well as by any derived class outside the assembly.
Example:

public class ExampleClass
{
    protected internal int exampleField; //protected internal field
    protected internal void ExampleMethod() //protected internal method
    {
        // method implementation
    }
}

public class DerivedClass : ExampleClass
{
    public void AccessProtectedInternal()
    {
        exampleField = 10; //accessing protected internal field from derived class outside assembly
        ExampleMethod(); //accessing protected internal method from derived class outside assembly
    }
}

6. private protected access modifiers:

C# 11 introduced a new access modifier called private protected. This access modifier is a combination of private and protected access modifiers. It allows a member to be accessed only within the same assembly, and only by derived types that are declared in the same assembly.

Example:

public class ExampleClass
{
    private protected int exampleField; //private protected field
    private protected void ExampleMethod() //private protected method
    {
        // method implementation
    }
}

public class DerivedClass : ExampleClass
{
    public void AccessPrivateProtected()
    {
        exampleField = 10; //accessing private protected field from derived class in the same assembly
        ExampleMethod(); //accessing private protected method from derived class in the same assembly
    }
}

// Another class in the same assembly
public class OtherClass
{
    public void AccessPrivateProtected()
    {
        ExampleClass exampleObject = new ExampleClass();
        exampleObject.exampleField = 20; // Not allowed as exampleField is private protected
        exampleObject.ExampleMethod(); // Not allowed as ExampleMethod is private protected
    }
}

In the above example, exampleField and ExampleMethod are declared as "private protected". They can be accessed by the derived class DerivedClass, but only if it is declared in the same assembly as ExampleClass. The OtherClass cannot access these members because it is not a derived class of ExampleClass and is not declared in the same assembly.

The private protected access modifier provides finer-grained control over the visibility of members, which can be useful in larger applications where different classes are developed by different teams or modules.

7. file access modifiers:

C# 11 introduces a new access modifier called file. When applied to a class, this access modifier restricts the visibility of the class to the current file only. This means that the class can only be accessed by code that is defined in the same file as the class.

Example:

File1.cs

file class ExampleClass
{
    public void ExampleMethod()
    {
        // method implementation
    }
}

File2.cs

using System;

class Program
{
    static void Main()
    {
        ExampleClass exampleObject = new ExampleClass(); // Not allowed as ExampleClass is file
        exampleObject.ExampleMethod(); // Not allowed as ExampleMethod is in ExampleClass which is file
    }
}


In the above example, ExampleClass is declared with the file access modifier. This means that it can only be accessed within the same file. The Main method in the Program class is in a different file, so it cannot access ExampleClass or its ExampleMethod.

The file access modifier provides a way to encapsulate a class and limit its visibility to the code that needs to access it. This can be useful in large applications where different teams or modules work on different parts of the code. It can also help to reduce the risk of naming conflicts between classes in different parts of the application.

To Summrised, These access modifiers help us to control the visibility and accessibility of classes, methods, fields, and other members within a program. By using the appropriate access modifier, we can ensure that our code is secure and maintainable.

A to Z Full Forms and Acronyms