Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are virtual functions in C#?
The virtual keyword in C# allows a method, property, indexer, or event to be overridden in derived classes. Virtual functions enable runtime polymorphism, where the actual method called is determined at runtime based on the object's type, not the reference type.
When you define a virtual function in a base class, derived classes can provide their own implementations using the override keyword. This allows different derived classes to implement the same method differently while maintaining a common interface.
Syntax
Following is the syntax for declaring a virtual method −
public virtual returnType MethodName() {
// base implementation (optional)
}
Following is the syntax for overriding a virtual method in a derived class −
public override returnType MethodName() {
// derived class implementation
}
How Virtual Functions Work
Virtual functions use dynamic binding or late binding. The method call is resolved at runtime based on the actual object type, not the reference type. This enables polymorphic behavior where the same method call can execute different implementations.
Example
using System;
namespace PolymorphismApplication {
class Shape {
protected int width, height;
public Shape(int a = 0, int b = 0) {
width = a;
height = b;
}
public virtual int area() {
Console.WriteLine("Parent class area:");
return 0;
}
}
class Rectangle : Shape {
public Rectangle(int a = 0, int b = 0) : base(a, b) {
}
public override int area() {
Console.WriteLine("Rectangle class area:");
return (width * height);
}
}
class Triangle : Shape {
public Triangle(int a = 0, int b = 0) : base(a, b) {
}
public override int area() {
Console.WriteLine("Triangle class area:");
return (width * height / 2);
}
}
class Caller {
public void CallArea(Shape sh) {
int a;
a = sh.area();
Console.WriteLine("Area: {0}", a);
}
}
class Tester {
static void Main(string[] args) {
Caller c = new Caller();
Rectangle r = new Rectangle(10, 7);
Triangle t = new Triangle(10, 5);
c.CallArea(r);
c.CallArea(t);
}
}
}
The output of the above code is −
Rectangle class area: Area: 70 Triangle class area: Area: 25
Virtual vs Abstract Methods
| Virtual Methods | Abstract Methods |
|---|---|
| Can have a default implementation in the base class | Must have no implementation in the base class |
| Derived classes can choose to override or inherit the base implementation | Derived classes must override abstract methods |
| The base class can be instantiated | The base class cannot be instantiated |
Calling Base Implementation
A derived class can call the base class implementation using the base keyword −
using System;
class Animal {
public virtual void MakeSound() {
Console.WriteLine("Animal makes a sound");
}
}
class Dog : Animal {
public override void MakeSound() {
base.MakeSound(); // Call base implementation first
Console.WriteLine("Dog barks");
}
}
class Program {
static void Main() {
Dog d = new Dog();
d.MakeSound();
}
}
The output of the above code is −
Animal makes a sound Dog barks
Conclusion
Virtual functions in C# enable runtime polymorphism by allowing derived classes to override base class methods. The actual method called is determined at runtime based on the object's type, providing flexibility and enabling different implementations while maintaining a common interface across the inheritance hierarchy.
