Tips & Ticks by SAJJAD
Posts tagged abstract
Abstract, Sealed & Static modifiers in C#
Oct 10th
C# provides many modifiers for use with types and type members. Of these, three can be used with classes: abstract, sealed and static.
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. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class
abstract class A {}
A a = new A(); //Error
sealed
When applied to a class, the sealed modifier prevents other classes from inheriting from it. In the following example, class B inherits from class A, but no class can inherit from class B.
sealed class A {}
class B:A {} //Error
static
The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes. The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created
static class A {}
A a = new A(); //Error
/* All member must be static */
Recent Comments