C# abstract method code example

Example 1: c# abstract class

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

class Square : Shape
{
    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

Example 2: what is abstract class in c#

Abstract class: is a restricted class that cannot be used to create objects 
(to access it, it must be inherited from another class).

Example 3: abstruct method c#

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

class Square : Shape
{
    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

Example 4: c# generic abstract method

public abstract class FilterBase<T>
{
    public abstract IQueryable<T> GetFilter(IQueryable<T> query);
}