Modifiers

abstract

  • The abstract modifier indicates that the thing being modified has a missing or incomplete implementation.

  • The abstract modifier can be used with classes, methods, properties, indexers, and events.

  • Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes, not instantiated on its own.

  • Members marked as abstract must be implemented by non-abstract classes that derive from the abstract class.

abstract class Shape {
    public abstract int GetArea();
}

class Square : Shape {
    private int _side;

    public Square(int n) => _side = n;

    // GetArea method is required to avoid a compile-time error.
    public override int GetArea() => _side * _side;

    static void Main() {
        var sq = new Square(12);
        Console.WriteLine($"Area of the square = {sq.GetArea()}");
    }
}
// Output: Area of the square = 144

const

  • Constants can be numbers, Boolean values, strings, or a null reference.

  • A constant expression is an expression that can be fully evaluated at compile time. Therefore, the only possible values for constants of reference types are string and a null reference.

  • The static modifier is not allowed in a constant declaration. By default, a const is static.

new

When used as a declaration modifier, the new keyword explicitly hides a member that is inherited from a base class.

readonly

  • In a field declaration, readonly indicates that assignment to the field can only occur as part of the declaration or in a constructor in the same class.

    A readonly field can be assigned and reassigned multiple times within the field declaration and constructor.

  • A readonly field can't be assigned after the constructor exits. This rule has different implications for value types and reference types:

    • Because value types directly contain their data, a field that is a readonly value type is immutable.

    • Because reference types contain a reference to their data, a field that is a readonly reference type must always refer to the same object. That object isn't immutable. The readonly modifier prevents the field from being replaced by a different instance of the reference type. However, the modifier doesn't prevent the instance data of the field from being modified through the read-only field.

readonly field cannot be declared inside the class, and are used at runtime.

readonly struct

  • In a readonly struct type definition, readonly indicates that the structure type is immutable.

  • In an instance member declaration within a structure type, readonly indicates that an instance member doesn't modify the state of the structure.

ref readonly

  • In a ref readonly method return, the readonly modifier indicates that method returns a reference and writes aren't allowed to that reference.

You can assign a value to a readonly field only in the following contexts:

  • When the variable is initialized in the declaration, for example:

  • In an instance constructor of the class that contains the instance field declaration.

  • In the static constructor of the class that contains the static field declaration.

These constructor contexts are also the only contexts in which it's valid to pass a readonly field as an out or ref parameter.

The readonly keyword differs from the const keyword.

A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used.

Also, although a const field is a compile-time constant, the readonly field can be used for run-time constants, as in this line: public static readonly uint l1 = (uint)DateTime.Now.Ticks;

sealed

  • The sealed modifier, when applied to a class prevents other classes from inheriting from it.

  • The sealed modifier can also be used on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties.

  • It is an error to use the abstract modifier with a sealed class, because an abstract class must be inherited by a class that provides an implementation of the abstract methods or properties.

  • When applied to a method or property, the sealed modifier must always be used with override.

  • Because structs are implicitly sealed, they cannot be inherited.

static

  • Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object.

  • A constant or type declaration is implicitly a static member.

  • While an instance of a class contains a separate copy of all instance fields of the class, there's only one copy (instance) of each static field.

  • If the static keyword is applied to a class, all the members of the class must be static.

Last updated

Was this helpful?