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.
const int X = 0;
public const double GravitationalConstant = 6.673e-11;
private const string ProductName = "Visual C#";
// using constant in interpolated strings expression
const string Language = "C#";
const string Platform = ".NET";
const string Version = "10.0";
const string FullProductName = $"{Platform} - Language: {Language} Version: {Version}";
// declaring multiple constants
public const double X = 1.0, Y = 2.0, Z = 3.0;
// using constant in constant expression
public const int C1 = 5;
public const int C2 = C1 + 100;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.
class SampleKeyword {
readonly int read = 10;
public SampleKeyword() {
read = 100;
}
public static void Main() {
SampleKeyword k = new SampleKeyword();
k.read = 200; // error
Console.WriteLine("read = {0}", k.read); // ok
}
}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.
private static readonly SamplePoint s_origin = new SamplePoint(0, 0, 0);
public static ref readonly SamplePoint Origin => ref s_origin;You can assign a value to a readonly field only in the following contexts:
When the variable is initialized in the declaration, for example:
public readonly int y = 5;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.
class A {}
sealed class B : A {} - // class B can't be inherited by any other classesThe
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.
class X {
protected virtual void F() { Console.WriteLine("X.F"); }
protected virtual void F2() { Console.WriteLine("X.F2"); }
}
class Y : X {
sealed protected override void F() { Console.WriteLine("Y.F"); }
protected override void F2() { Console.WriteLine("Y.F2"); }
}
class Z : Y {
// Attempting to override F causes compiler error CS0239.
protected override void F() { Console.WriteLine("Z.F"); }
// Overriding F2 is allowed.
protected override void F2() { Console.WriteLine("Z.F2"); }
}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?