Understanding Virtual, Override and New Keywords in C#

Krunal Shah

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");
    }
}

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");
    }
}

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");
    }
}

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");
    }
}

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#.


More Stories

SOLID principles in C# with examples

SOLID principles are a set of design principles that can be used to improve the quality of Csharp code. By following these principles, developers can create code that is more flexible, readable, testable, maintainable, and reusable.

Krunal Shah

How To Enable Gzip Compression In Asp Net Core Using Dot Net CLI With Example

GZIP is a generic compression method that can be applied to any stream of bits, need to do some configration changes in startup file in Dot Net core CLI 6

Krunal Shah

Exploring the Benefits of Using Partial Methods in C#: A Beginners Guide

Learn about Partial Methods in C#: a powerful feature for customizing auto-generated code, split method declaration and implementation within partial class or struct, with many options. #CSharp #PartialMethods

Krunal Shah