Modifiers and Access Specifiers in C#


In this article we are going to discuss Modifiers and Access Specifiers in C#. There are many blogs, articles are available on the internet regarding Modifiers and Access Specifiers but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Modifiers and Access Specifiers use in C#.

In C#, modifiers and access specifiers are often used interchangeably, but they refer to different concepts. Here’s a breakdown of the differences:

Access Specifiers (Access Modifiers)

Access specifiers (also known as access modifiers) define the visibility and accessibility of classes, methods, and other members. They control which parts of your code can see and use certain components. The main access specifiers in C# are:

  1. private - The member is accessible only within the same class or struct.
  2. protected - The member is accessible within the same class or struct, and by derived class instances.
  3. internal - The member is accessible within the same assembly, but not from another assembly.
  4. protected internal - The member is accessible within its own assembly or from derived classes in another assembly.
  5. public - The member is accessible from any other code.

Example

public class MyClass
{
    private int privateField; // Accessible only within MyClass
    protected int protectedField; // Accessible within MyClass and derived classes
    internal int internalField; // Accessible within the same assembly
    protected internal int protectedInternalField; // Accessible within the same assembly or derived classes
    public int publicField; // Accessible from any code
}

Modifiers

Modifiers are keywords that provide additional information about the behavior and characteristics of classes, methods, properties, and other members. They do not necessarily relate to accessibility but to the behavior and usage of the members. Some common modifiers in C# include:

  1. static - Indicates that the member belongs to the type itself rather than to a specific object.
  2. abstract - Indicates that the class or member is incomplete and must be implemented in a derived class.
  3. sealed - Prevents a class from being inherited.
  4. virtual - Indicates that a method or property can be overridden in a derived class.
  5. override - Indicates that a method or property is overriding a virtual method or property in a base class.
  6. readonly - Indicates that the field can only be assigned during initialization or in a constructor.
  7. const - Indicates that the value of the field is constant and cannot be changed.
  8. async - Indicates that a method is asynchronous and can use await within its body.
  9. partial - Indicates that the class, struct, or interface can be split across multiple files.

Example

public class MyClass
{
    public static int staticField; // Static field
    public readonly int readonlyField; // Readonly field
    public const int constantField = 42; // Constant field

    public virtual void VirtualMethod() { } // Virtual method
    public abstract void AbstractMethod(); // Abstract method (only in abstract classes)
    public sealed void SealedMethod() { } // Sealed method (prevents further overriding)
}

public partial class PartialClass // Partial class definition (could be in another file too)
{
    // Partial implementation
}

Difference

  1. Access specifiers determine the visibility and accessibility of a class or member.
  2. Modifiers provide additional information about how a class or member behaves or is used.

Next