0
0
CsharpComparisonBeginner · 4 min read

Override vs new in C#: Key Differences and When to Use Each

In C#, override modifies a base class method to provide a new implementation used in polymorphism, while new hides a base class member without affecting polymorphism. Use override to change behavior in derived classes, and new to hide members when polymorphism is not needed.
⚖️

Quick Comparison

This table summarizes the main differences between override and new keywords in C#.

Aspectoverridenew
PurposeReplace base method implementation for polymorphismHide base member without polymorphism
Requires base methodYes, base method must be virtual, abstract, or overrideNo, can hide any base member
Effect on polymorphismSupports runtime polymorphismDoes not affect polymorphism; hides member at compile time
Compiler warningNo warning if used correctlyWarning if base member is virtual and new hides it
Call via base referenceCalls derived override methodCalls base method, not hidden one
Use caseChange behavior in derived classHide base member intentionally
⚖️

Key Differences

The override keyword in C# is used to extend or modify the behavior of an inherited virtual or abstract method. It enables polymorphism, meaning that when you call the method on a base class reference, the derived class's version runs. This is essential for dynamic behavior changes in object-oriented programming.

On the other hand, the new keyword hides a member of the base class with a new member in the derived class. This does not participate in polymorphism. If you call the member through a base class reference, the base class version executes, not the new one. The new keyword is useful when you want to intentionally hide a base member without overriding its behavior.

Using override requires the base method to be marked as virtual, abstract, or already override. The new keyword can hide any base member, virtual or not, but it may cause compiler warnings if the base member is virtual. Understanding these differences helps you control method behavior and inheritance clearly.

⚖️

Code Comparison

Here is an example showing how override changes the behavior of a base class method.

csharp
using System;

class BaseClass
{
    public virtual void Show()
    {
        Console.WriteLine("BaseClass Show method");
    }
}

class DerivedOverride : BaseClass
{
    public override void Show()
    {
        Console.WriteLine("DerivedOverride Show method");
    }
}

class Program
{
    static void Main()
    {
        BaseClass obj = new DerivedOverride();
        obj.Show();
    }
}
Output
DerivedOverride Show method
↔️

new Equivalent

This example shows how new hides the base class method instead of overriding it.

csharp
using System;

class BaseClass
{
    public void Show()
    {
        Console.WriteLine("BaseClass Show method");
    }
}

class DerivedNew : BaseClass
{
    public new void Show()
    {
        Console.WriteLine("DerivedNew Show method");
    }
}

class Program
{
    static void Main()
    {
        BaseClass obj = new DerivedNew();
        obj.Show();
    }
}
Output
BaseClass Show method
🎯

When to Use Which

Choose override when you want to change or extend the behavior of a base class method and support polymorphism. This is common in object-oriented designs where derived classes provide specific implementations.

Choose new when you want to hide a base class member intentionally without affecting polymorphism, such as when the base member is not virtual or when you want to avoid overriding behavior but provide a different member in the derived class.

Using override ensures dynamic method dispatch, while new keeps calls to base references using the base implementation.

Key Takeaways

override modifies virtual base methods to enable polymorphism.
new hides base members without changing polymorphic behavior.
override requires a virtual or abstract base method; new does not.
Calls through base references use override methods but not new methods.
Use override to change behavior; use new to hide members intentionally.