April 20, 2010

Virtual functions in C#

Virtual functions implement the concept of polymorphism are the same as in C# except that you use the override keyword with the virtual function implementaion in the child class. The parent class uses the same virtual keyword. Every class that overrides the virtual method will use the override keyword.

class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Shape.Draw") ;
    }
}


class Rectangle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Rectangle.Draw");
    }
}

No comments: