DEV Community

Cover image for Understanding Virtual, Override and New Keywords in C#
Krunal Shah
Krunal Shah

Posted on

Understanding Virtual, Override and New Keywords in C#

Understanding Virtual, Override and New Keywords in C#


Introduction
As a C# developer, it's essential to have a deep understanding of the virtual, override, and new keywords. These keywords are used to control how methods and properties in a class hierarchy behave and interact with each other. In this article, we will explore the differences between these keywords and when to use them.

Virtual Keyword
The virtual keyword is used to indicate that a method or property can be overridden in a derived class. When a method or property is declared as virtual, it can be overridden by any class that derives from the class that contains the virtual method or property.

Here's an example of a class that contains a virtual method:

class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a shape");
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the Draw() method is marked as virtual. This means that any class that inherits from the Shape class can override the Draw() method to provide its own implementation.

For example, the following class overrides the Draw() method to provide its own implementation:

class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a circle");
    }
}
Enter fullscreen mode Exit fullscreen mode

When the Draw() method is called on an instance of the Circle class, the overridden implementation will be called.

Override Keyword
The override keyword is used to indicate that a method or property in a derived class is intended to override a virtual method or property in the base class.

Here's an example of a class that overrides a virtual method:

class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a circle");
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the Draw() method in the Circle class is marked as override. This tells the compiler that the method is intended to override the virtual Draw() method in the Shape class.

If the method in the base class is not virtual, the compiler will raise an error.

It's important to note that the override keyword can only be used to override virtual or abstract methods and properties.

New Keyword
The new keyword is used to indicate that a method or property in a derived class is intended to hide, rather than override, a method or property in the base class.

Here's an example of a class that uses the new keyword to hide a method:

class Circle : Shape
{
    public new void Draw()
    {
        Console.WriteLine("Drawing a circle");
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the Draw() method in the Circle class is marked as new. This tells the compiler that the method is intended to hide, rather than override, the Draw() method in the Shape class.

When the Draw() method is called on an instance of the Circle class, the implementation in the Circle class will be called, rather than the implementation in the Shape class.

It's important to note that the new keyword can be used to hide any method or property, regardless of whether it's virtual or not.

Conclusion
In C#, the virtual, override, and new keywords are used to control how the inherited members are accessed and implemented in derived classes. Virtual methods can be overridden by derived classes, override methods provide a new implementation for virtual methods, and new methods hide inherited methods with the same name and signature. Understanding these keywords and how to use them correctly is essential for creating robust and maintainable object-oriented code in C#.

Top comments (1)

Collapse
 
ant_f_dev profile image
Anthony Fung

Thanks for sharing! A great explanation of important concepts in inheritance and polymorphism.