In C#, an abstract class is a class that cannot be instantiated on its own but can be used as a base class for other classes. The purpose of an abstract class is to provide a common base implementation that derived classes can inherit and extend.
An abstract class is declared using the abstract
keyword. It can contain both abstract and non-abstract members. An abstract member is a member that does not have an implementation in the abstract class, and must be implemented in the derived class. A non-abstract member is a member that has an implementation in the abstract class, and can be used as-is by derived classes.
Here is an example of an abstract class:
public abstract class Shape
{
public abstract double Area();
public abstract double Perimeter();
public void Display()
{
Console.WriteLine($"Area: {Area()}");
Console.WriteLine($"Perimeter: {Perimeter()}");
}
}
In this example, Rectangle is a class that derives from Shape. It implements the Area() and Perimeter() methods, and inherits the Display() method from Shape.Note that you cannot create an instance of an abstract class directly. You can only create an instance of a derived class that extends the abstract class.