C# - Abstract Method

Abstract Method

When an instance method declaration includes an abstract modifier, that method is said to be an abstract method. Although an abstract method is implicitly also a virtual method, it cannot have the modifier virtual.

An abstract method declaration introduces a new virtual method but does not provide an implementation of that method. Instead, non-abstract derived classes are required to provide their own implementation by overriding that method. Because an abstract method provides no actual implementation, the method-body of an abstract method simply consists of a semicolon.

Abstract method declarations are only permitted in abstract classes

Abstract Method in c# Simple Example

using System;
namespace MethodOverridingUsingAbstractMethodDemo
{
    abstract class shape
    {
        public abstract void draw();
    }
    class circle : shape
    {
        public override void draw()
        {
            Console.WriteLine("Circle Drawing...");
        }
    }
    class rect : shape
    {
        public override void draw()
        {
            Console.WriteLine("rect Drawing...");
        }
    }
    class triangle : shape
    {
        public override void draw()
        {
            Console.WriteLine("triangle Drawing...");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            shape r;
            circle c = new circle();
            rect rt = new rect();
            r = c;
            r.draw();
            r = rt;
            r.draw();
            r = new triangle();
            r.draw();
            shape[] r_arr = new shape[5];
            r_arr[0] = new circle();
            r_arr[1] = new triangle();
            r_arr[2] = new circle();
            r_arr[3] = new rect();
            r_arr[4] = new triangle();
            Console.WriteLine("Drawing Shapes using array of References");
            for (int i = 0; i < 5; i++)
                r_arr[i].draw();
        }
    }
}

Output - Abstract Method in c# Simple Example

Circle Drawing...
rect Drawing...
triangle Drawing...
Drawing Shapes using array of References
Circle Drawing...
triangle Drawing...
Circle Drawing...
rect Drawing...
triangle Drawing...