Summary: in this tutorial, you’ll learn how to use the C# virtual modifier and how to use it to define a virtual method.
Introduction to the C# virtual modifier
In inheritance, a class (subclass) inherits from another class (base class). If a member in the subclass has the same name as the one in the base class, the member in the subclass replaces (or hides) the member in the base class.
For example:
class Person
{
public string Name { get; set; }
public string Introduce() => $"Hi, I'm {Name}.";
}
class Employee : Person
{
public string JobTitle { get; set; }
public new string Introduce() => $"Hi, I'm {Name}. I'm a {JobTitle}.";
}
Code language: C# (cs)
In this example, the Introduce()
method in the Employee
class hides the Introduce()
method in the Person
class. The new
modifier explicitly communicates our intent.
However, if you want a member in the subclass to override the member with the same name in the base class, you need to:
- First, use the
virtual
modifier in the declaration of the member of the base class - Second, use the
override
modifier in the declaration of the member in the subclass.
For example:
class Person
{
public string Name { get; set; }
public virtual string Introduce() => $"Hi, I'm {Name}.";
}
class Employee : Person
{
public string JobTitle { get; set; }
public override string Introduce() => $"Hi, I'm {Name}. I'm a {JobTitle}.";
}
Code language: C# (cs)
In this example:
- Use the
virtual
keyword to modify the declaration of theIntroduce()
method in thePerson
class. - Use the
override
keyword to modify the declaration of theIntroduce()
method in theEmployee
class.
Differences between new and override modifiers
Let’s take an example to understand the difference between the new and override modifiers.
The override method
The override
modifier instructs the C# compiler to use the last defined implementation of a method; even if you call the method on a reference of the base class. For example:
class Person
{
public string Name { get; set; }
public virtual string Introduce() => $"Hi, I'm {Name}.";
}
class Employee : Person
{
public string JobTitle { get; set; }
public override string Introduce() => $"Hi, I'm {Name}. I'm a {JobTitle}.";
}
Code language: C# (cs)
// Program.cs
var employee = new Employee()
{
Name = "John Doe",
JobTitle = "C# Developer"
};
Console.WriteLine(employee.Introduce());
Person person = employee;
Console.WriteLine(person.Introduce());
Code language: C# (cs)
Output:
Hi, I'm John Doe. I'm a C# Developer.
Hi, I'm John Doe. I'm a C# Developer.
Code language: plaintext (plaintext)
In this example, when we call the Introduce() method, the compiler executes the method defined in the Employee class as expected.
And when we call the Introduce() method on an instance of the Person class, the compiler also executes the Introduce() method defined in the Employee class.
The new method
The new
modifier instructs the C# compiler to use the method defined in the subclass. However, if you call the method on an instance of the base class, the compiler will use the method defined in the base class instead, not the one defined in the subclass. For example:
class Person
{
public string Name { get; set; }
public string Introduce() => $"Hi, I'm {Name}.";
}
class Employee : Person
{
public string JobTitle { get; set; }
public new string Introduce() => $"Hi, I'm {Name}. I'm a {JobTitle}.";
}
Code language: C# (cs)
// Program.cs
var employee = new Employee()
{
Name = "John Doe",
JobTitle = "C# Developer"
};
Console.WriteLine(employee.Introduce());
Person person = employee;
Console.WriteLine(person.Introduce());
Code language: C# (cs)
Output:
Hi, I'm John Doe. I'm a C# Developer.
Hi, I'm John Doe.
Code language: plaintext (plaintext)
When we call the Introduce()
method on an instance of the Employee
class, the compiler executes the method defined in the Employee
class. This behavior is the same as in the override method example.
However, when we call the Introduce()
method on an instance of the Person
class, the compiler executes the method defined in the Person
class. This is the difference.
Calling a method of the base class
To explicitly call a method from a base class from a subclass, you use the base
keyword.
For example, The following illustrates how to call the Introduce()
method of the Person
class in the Introduce()
method of the Employee
class:
class Person
{
public string Name { get; set; }
public virtual string Introduce() => $"Hi, I'm {Name}.";
}
class Employee : Person
{
public string JobTitle { get; set; }
public override string Introduce() => $"{base.Introduce()} I'm a {JobTitle}.";
}
Code language: C# (cs)
In this example, we reference the Introduce()
method of the Person
class in the Introduce() method of the Employee class using the base
keyword:
base.Introduce()
Code language: CSS (css)
If we did not do so, we would have a recursive method because the Introduce()
method would call itself.
Summary
- Use the C#
virtual
modifier for a member in the base class if you want to override it in the member with the same name of the subclasses. - Use the base keyword to explicitly reference the method in a base class from a subclass.