What is Virtual Method in C#
Virtual methods in C# are methods that are declared in a base class and can be overridden by derived classes. In other words, a virtual method is a method that can be redefined in a subclass, here is the syntax for it:
{
// method implementation
}
The virtual keyword indicates that this method can be overridden by a derived class and when a derived class overrides a virtual method, it provides its own implementation of the method.
To override a virtual method in a derived class, you include the override keyword in the method signature and below is the syntax for it:
{
// method implementation
}
The override keyword indicates that this method is overriding a virtual method in the parent class. Here is an example code snippet that demonstrates the use of virtual methods in C#:
// Define a base class named Calculation
class Calculation
{
// Define a virtual Add method that takes two integers and returns an integer result
public virtual int Add(int x, int y)
{
return x + y; // Return the sum of the two input integers
}
}
// Define a derived class named CustomCalculation that inherits from Calculation
class CustomCalculation : Calculation
{
// Override the base class's Add method
public override int Add(int x, int y)
{
if (x > 10 || y > 10) // If either of the input integers is greater than 10
{
return x - y; // Subtract y from x and return the result
}
else // If neither of the input integers is greater than 10
{
return base.Add(x, y); // Call the base class's Add method to return the sum of the two input integers
}
}
}
class Program
{
static void Main(string[] args)
{
// Create an instance of the base class named Calculation
Calculation calc = new Calculation();
// Create an instance of the derived class named CustomCalculation
CustomCalculationcustomCalc = new CustomCalculation();
// Call the virtual Add method on the base Calculation object
int result1 = calc.Add(5, 7);
Console.WriteLine($"Result1: {result1}"); // Output: Result1: 12
// Call the overridden Add method on the CustomCalculation object
int result2 = customCalc.Add(12, 7);
Console.WriteLine($"Result2: {result2}"); // Output: Result2: 5
}
}
In this example, we have a base class called Calculation with a virtual method called Add that simply adds two integers together. We also have a derived class called CustomCalculation that overrides the Add method, but only when the sum of the two numbers is greater than 10.
In the overridden Add method, we check if either x or y is greater than 10. If this condition is true, we return the difference between x and y and if the condition is false, we call the base implementation of the Add method using the base keyword.
In the Main method, we create a Calculation object and a CustomCalculation object. We first call the virtual Add method on the base Calculation object, passing in 5 and 7 as arguments. This should result in a sum of 12 and we then call the overridden Add method on the CustomCalculation object, passing in 12 and 7 as arguments. This should result in a difference of 5, since the sum of the two numbers is greater than 10.
As you can see, the overridden Add method in the CustomCalculation class is only called when the condition is false. When the condition is true, the overridden method provides a different implementation of the Add method that subtracts the second number from the first, below is the output of the code:
Conclusion
The virtual method in C# provides a way for classes to define methods that can be customized by derived classes. By marking a method as virtual, you allow derived classes to provide their own implementation of the method and his is a powerful feature of object-oriented programming that allows for greater flexibility and reusability in code.