Practical usage of virtual functions in c#

So basically if in your ancestor class you want a certain behaviour for a method. If your descendent uses the same method but has a different implementation you can override it, If it has a virtual keyword.

using System;
class TestClass 
{
   public class Dimensions 
   {
      public const double pi = Math.PI;
      protected double x, y;
      public Dimensions() 
      {
      }
      public Dimensions (double x, double y) 
      {
         this.x = x;
         this.y = y;
      }

      public virtual double Area() 
      {
         return x*y;
      }
   }

   public class Circle: Dimensions 
   {
      public Circle(double r): base(r, 0) 
      {
      }

      public override double Area() 
      { 
         return pi * x * x; 
      }
   }

   class Sphere: Dimensions 
   {
      public Sphere(double r): base(r, 0) 
      {
      }

      public override double Area()
      {
         return 4 * pi * x * x; 
      }
   }

   class Cylinder: Dimensions 
   {
      public Cylinder(double r, double h): base(r, h) 
      {
      }

      public override double Area() 
      {
         return 2*pi*x*x + 2*pi*x*y; 
      }
   }

   public static void Main()  
   {
      double r = 3.0, h = 5.0;
      Dimensions c = new Circle(r);
      Dimensions s = new Sphere(r);
      Dimensions l = new Cylinder(r, h);
      // Display results:
      Console.WriteLine("Area of Circle   = {0:F2}", c.Area());
      Console.WriteLine("Area of Sphere   = {0:F2}", s.Area());
      Console.WriteLine("Area of Cylinder = {0:F2}", l.Area());
   }
}

Edit: Questions in comment
If I don't use virtual keyword in base class, will it work?

If you use the override keyword in your descendent classes it will not work. You will generate compiler error CS0506 'function1' : cannot override inherited member 'function2' because it is not marked "virtual", "abstract", or "override"

If you don't use the override You'll get the CS0108 warning 'desc.Method()' hides inherited member 'base.Method()' Use the new keyword if hiding was intended.

To get around this put the new keyword in front of the method you are hiding.

e.g.

  new public double Area() 
  {
     return 2*pi*x*x + 2*pi*x*y; 
  }

..and is it compulsory to override a virtual method in derived class?
No, if you don't override the method, the descendent class will use method it is inheriting from.


The key to understanding the practical usage of virtual functions is to keep in mind that an object of a certain class can be assigned another object of a class derived from the first object's class.

E.g.:

class Animal {
   public void eat() {...}
}

class FlyingAnimal : Animal {
   public void eat() {...}
}

Animal a = new FlyingAnimal();

The Animal class has a function eat() that generally describes how an animal should eat (e.g. put the object in mouth and swallow).

However, the FlyingAnimal class should define a new eat() method, because flying animals have a particular way of eating.

So the question that comes to mind here is: after I declared the variable a of type Animal and asigned it a new object of type FlyingAnimal, what will a.eat() do? Which of the two methods is called?

The answer here is: because a is of type Animal, it will call Animal's method. The compiler is dumb and doesn't know that you are going to assign an object of another class to the a variable.

Here is where the virtual keyword comes in action: if you declare the method as virtual void eat() {...}, you are basically telling the compiler "be careful that I am doing some clever stuff here that you cannot handle because you're not as smart". So the compiler will not attempt to link the call a.eat() to either of the two methods, but instead it tells the system to do it at runtime!

So only when the code executes, the system will look at a's content type not at its declared type and executes FlyingAnimal's method.

You may wonder: why the hell would I want to do that? Why not say right from the start FlyingAnimal a = new FlyingAnimal() ?

The reason for that is that, for example, you may have many derived classes from Animal: FlyingAnimal, SwimmingAnimal, BigAnimal, WhiteDog etc. And at one point you want to define a world containing many Animals, so you say:

Animal[] happy_friends = new Animal[100];

We have a world with 100 happy animals. You initialize them at some point:

...
happy_friends[2] = new AngryFish();
...
happy_friends[10] = new LoudSnake();
...

And at the end of the day, you want everybody to eat before they go to sleep. So you want to say:

for (int i=0; i<100; i++) {
   happy_friends[i].eat();
}

So as you can see, each animal has its own method of eating. Only by using virtual functions can you achieve this functionality. Otherwise, everyone would be forced to "eat" in the exact same way: as described in the most general eat function inside the Animal class.

EDIT: This behavior is actually default in common high-level languages like Java.


Like any other language..when you want polymorphism. There are tons of usage for this. For example you want to abstract the way input is read from a console or a file or some other device. You can have a generic reader interface followed by multiple concrete implementations using virtual functions.


e.g. proxying methods. i.e. overwriting methods at runtime. For example, NHibernate uses this to support lazy loading.