Modifiers
abstract
abstractThe
abstractmodifier 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
abstractmodifier 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
abstractmust 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 = 144const
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
stringand a null reference.The
staticmodifier is not allowed in a constant declaration. By default, aconstis static.
new
newWhen used as a declaration modifier, the new keyword explicitly hides a member that is inherited from a base class.
readonly
readonlyIn a field declaration,
readonlyindicates 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
readonlyfield 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
readonlyvalue type is immutable.Because reference types contain a reference to their data, a field that is a
readonlyreference type must always refer to the same object. That object isn't immutable. Thereadonlymodifier 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 struct
In a
readonly structtype definition,readonlyindicates that the structure type is immutable.In an instance member declaration within a structure type,
readonlyindicates that an instance member doesn't modify the state of the structure.
ref readonly
In a
ref readonlymethod return, thereadonlymodifier 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
readonlyfield as an out or ref parameter.
sealed
sealedThe
sealedmodifier, when applied to a class prevents other classes from inheriting from it.
The
sealedmodifier 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
abstractmodifier 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
sealedmodifier must always be used with override.Because structs are implicitly sealed, they cannot be inherited.
static
staticUse the
staticmodifier 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
staticmember.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
staticfield.If the
statickeyword is applied to a class, all the members of the class must bestatic.
Last updated
Was this helpful?