What are Virtual Methods?

C#VirtualKeyword

C# Problem Overview


Why would you declare a method as "virtual".

What is the benefit in using virtual?

C# Solutions


Solution 1 - C#

The Virtual Modifier is used to mark that a method\property(ect) can be modified in a derived class by using the override modifier.

Example:

class A
{
    public virtual void Foo()
       //DoStuff For A
}

class B : A
{
    public override void Foo()
    //DoStuff For B

    //now call the base to do the stuff for A and B 
    //if required
    base.Foo()
}

Solution 2 - C#

Virtual allows an inheriting class to replace a method that the base class then uses.

public class Thingy
{
    public virtual void StepA()
    {
        Console.Out.WriteLine("Zing");
    }

    public void Action()
    {
        StepA();
        Console.Out.WriteLine("A Thingy in Action.");
    }
}

public class Widget : Thingy
{
    public override void StepA()
    {
        Console.Out.WriteLine("Wiggy");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Thingy thingy = new Thingy();
        Widget widget = new Widget();

        thingy.Action();
        widget.Action();

        Console.Out.WriteLine("Press any key to quit.");
        Console.ReadKey();
    }
 }

When you run the Program your output will be:

Zing 
A Thingy in Action. 
Wiggy 
A Thingy in Action.

Notice how even though Widget called the Action() method defined at the Thingy level, internally Thingy called Widget's StepA() method.

The basic answer is it gives inheritors of a class more flexibility. Of course, you've got to engineer your class well or it could weak havoc.

Solution 3 - C#

A virtual method is a type of method where the actual method calls depends on the runtime type of the underlying object.

A non-virtual method is a type of method where the actual method called depends on the reference type of the object at the point of method invocation.

Solution 4 - C#

Virtual Methods on MSDN

> The virtual keyword is used to modify > a method or property declaration, in > which case the method or the property > is called a virtual member. The > implementation of a virtual member can > be changed by an overriding member in > a derived class. > > When a virtual method is invoked, the > run-time type of the object is checked > for an overriding member. The > overriding member in the most derived > class is called, which might be the > original member, if no derived class > has overridden the member. (For more > information on run-time type and most > derived implementation, see 10.5.3 > Virtual methods.) > > By default, methods are non-virtual. > You cannot override a non-virtual > method. > > You cannot use the virtual modifier > with the following modifiers: > > static abstract override > > Virtual properties behave like > abstract methods, except for the > differences in declaration and > invocation syntax. > > - It is an error to use the virtual modifier on a static property. > - A virtual inherited property can be overridden in a derived class by > including a property declaration that > uses the override modifier.

Solution 5 - C#

Virtual methods are similar to abstract methods in base classes except their implementation on derived classes is optional. Also you could put logic in virtual method and override these in derived classes.

Solution 6 - C#

Even if you don't plan to derive from the class, marking the method virtual may be necessary in order to mock the class. Some mocking frameworks only allow you to mock virtual methods. Note that methods implementing an interface are virtual implicitly.

I use RhinoMocks which has this restriction and have taken to marking my methods virtual by default for just this reason. For me, this is probably the biggest reason to use virtual methods as the cases where inheritance comes into play are much less frequent.

Solution 7 - C#

A short question, a short answer! Qualify your method as "virtual" if you think you will inherit of the class it belongs to.

A longer answer: "virtual enables you to override, to give another meaning of your method in a derived class.

Solution 8 - C#

In order to be able to override it in inheriting classes.

Check out the MSDN entry for the keyword. That explains it more in depth.

Solution 9 - C#

In C#, for overriding the base class method in derived class, you have to declare base class method as virtual and derived class method as override as shown below:

using System;
namespace Polymorphism
{
 class A
 {
 public virtual void Test() { Console.WriteLine("A::Test()"); }
 }
 
 class B : A
 {
 public override void Test() { Console.WriteLine("B::Test()"); }
 }
 
 class C : B
 {
 public override void Test() { Console.WriteLine("C::Test()"); }
 }
 
 class Program
 {
 static void Main(string[] args)
 {
 
 A a = new A();
 B b = new B();
 C c = new C();
 a.Test(); // output --> "A::Test()"
 b.Test(); // output --> "B::Test()"
 c.Test(); // output --> "C::Test()"
 
 a = new B();
 a.Test(); // output --> "B::Test()"
 
 b = new C();
 b.Test(); // output --> "C::Test()"
 
 Console.ReadKey();
 }
 }
}

You can also mix the method hiding and method overriding by using virtual and new keyword since the method of a derived class can be virtual and new at the same time. This is required when you want to further override the derived class method into next level as I am overriding Class B, Test() method in Class C as shown below:

using System;
namespace Polymorphism
{
 class A
 {
 public void Test() { Console.WriteLine("A::Test()"); }
 }
 
 class B : A
 {
 public new virtual void Test() { Console.WriteLine("B::Test()"); }
 }
 
 class C : B
 {
 public override void Test() { Console.WriteLine("C::Test()"); }
 }
 
 class Program
 {
 static void Main(string[] args)
 {
 
 A a = new A();
 B b = new B();
 C c = new C();
 
 a.Test(); // output --> "A::Test()"
 b.Test(); // output --> "B::Test()"
 c.Test(); // output --> "C::Test()"
 
 a = new B();
 a.Test(); // output --> "A::Test()"
 
 b = new C();
 b.Test(); // output --> "C::Test()"
 
 Console.ReadKey();
 }
 }
}

GOLDEN WORDS: The virtual keyword is used to modify a method, property, indexer, or event declared in the base class and allow it to be overridden in the derived class.

The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into derived class.

The new keyword is used to hide a method, property, indexer, or event of base class into derived class.

ENJOY :-)

Solution 10 - C#

Needless to say, virtual methods come in handy when your code is trying to abide with the Open Closed Principle

Read More about the Open Closed Principle here, Uncle Bob's original OCP whitepaper.

Also pls be aware that methods are not virtual by default in C# unlike Java.

Solution 11 - C#

Virtual functions are the functions that doesn't really exists.The derived class can modify the virtual function by overriding it.Virtual functions are one of the way to achieve run time polymorphism

    public class sample {
      public virtual void fun(){
        Console.WriteLine("base sample class \n");
      }
    }
    public class A : sample{
      public override void fun(){
        Console.WriteLine("Class A \n");
      }
    }
    public class B : sample{
      public override void fun(){
        Console.WriteLine("Class B \n");
      }
    }
    class run{
      public static void main(String[] args){
        sample obj = new sample();
        sample obj1 = new A();
        sample obj2 = new B();
        obj.fun();
        obj1.fun();
        obj2.fun();
      }
    }

Solution 12 - C#

Here it is explained clearly with example C# Virtual Method

Solution 13 - C#

The runtime takes place over compile time.
When you declare a method as virtual, declaring it in derived class require you to add a override or new modifier.
we can see that when TrySpeak. Passing in child and father, both call Speak of father, while TryScream, would call each method.
To understand this, there are some things we should know, in an instance of Child, There are two Scream methods from Child class or Father class. We could either call the Scream from Child class or Father class. Because Virtaul Modifier mark the method so it can be overriding by the derived class, which means even the Scream is called from Father class, it is overriden, it would be defferent if you use new modifier.

using System;
class Father
{
    Speak()
    {
  	    Console.Writeline("Father is speaking")	
    }
    virtual Scream()
    {
      	Console.Writeline("Father is screaming")	
    }
}
class Child: father
{
    Speak()
    {
  	    Console.Writeline("Child is speaking")	
    }
    override Scream()
    {
  	    Console.Writeline("Child is screaming")	
    }
}
class APP
{
    public static void Main()
    {
        // We new two instances here
        Father father = new Father();
        Child child = new Child();        
        // Here we call their scream or speak through TryScream or TrySpeak
        TrySpeak(father);
        TrySpeak(child);
        //>>>"Father is speaking"
        //>>>"Father is speaking"
        TryScream(father);
        TryScream(child);
        //>>>"Father is screaming"
        //>>>"Child is screaming"
    }
    // when your method take an Parameter who type is Father
    // You can either pass in a Father instance or
    // A instance of a derived Class from Father
    // which could be Child
    public static void TrySpeak(Father person)
    {
        person.Scream();
    }
    public static void TryScream(Father person)
    {
        person.Speak();
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmtnclimber10View Question on Stackoverflow
Solution 1 - C#cgreenoView Answer on Stackoverflow
Solution 2 - C#LudingtonView Answer on Stackoverflow
Solution 3 - C#JaredParView Answer on Stackoverflow
Solution 4 - C#Russ CamView Answer on Stackoverflow
Solution 5 - C#cycloView Answer on Stackoverflow
Solution 6 - C#tvanfossonView Answer on Stackoverflow
Solution 7 - C#Stephane HalimiView Answer on Stackoverflow
Solution 8 - C#PHeibergView Answer on Stackoverflow
Solution 9 - C#Rana BhopaleView Answer on Stackoverflow
Solution 10 - C#abhilashView Answer on Stackoverflow
Solution 11 - C#Lineesh K MohanView Answer on Stackoverflow
Solution 12 - C#HitsaView Answer on Stackoverflow
Solution 13 - C#patpatView Answer on Stackoverflow