Method Overriding in C#: virtual, override, base and new (.NET 10)

If you’ve written a Fee() method in a C# derived class and called it through a base-class variable, you’ve probably watched the base version run anyway, with a CS0108 warning in the build output.

That’s because method overriding in C# takes two keywords: virtual on the base method and override on the derived one. Leave either out and you’ve only hidden the method.

Below, I fix that on .NET 10 with one checkout example, then add base calls, abstract and sealed overrides, ToString() and covariant returns.

The Sample Checkout We’ll Use

Every example charges the same $120.00 order at an Austin, Texas, bike shop. A Payment base class has a Fee() method, and each payment type in the C# inheritance hierarchy prices it differently:

ClassProcessing feeFee on $120.00
Payment (base)None$0.00
Card2.9% + $0.30$3.78
AchFlat $0.80$0.80
PayPal3.49% + $0.49$4.68

The tricky row is the base class. Whenever you see $0.00 in an output below, the base method ran instead of the override.

Why Does the Base Method Run Instead of Your Override?

Here’s the code most people write first. Card declares its own Fee() and Name(), but nothing says override:

Payment pay = new Card();
Card card = new Card();
Console.WriteLine($"Through Payment: ${pay.Fee(120m):0.00}");
Console.WriteLine($"Through Card:    ${card.Fee(120m):0.00}");
Console.WriteLine($"pay.Name():      {pay.Name()}");

class Payment
{
    public decimal Fee(decimal amount) => 0m;
    public virtual string Name() => "Payment";
}
class Card : Payment
{
    public decimal Fee(decimal amount) => amount * 0.029m + 0.30m;
    public string Name() => "Card";
}

Build warnings and output:

C:\DotNetDemo\OverrideDemo\Hiding.cs(14,20): warning CS0108: 'Card.Fee(decimal)' hides inherited member 'Payment.Fee(decimal)'. Use the new keyword if hiding was intended.
C:\DotNetDemo\OverrideDemo\Hiding.cs(15,19): warning CS0114: 'Card.Name()' hides inherited member 'Payment.Name()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
Through Payment: $0.00
Through Card:    $3.78
pay.Name():      Payment
C# method hiding in VS Code: warnings CS0108 and CS0114, and a Payment variable holding a Card still returns the base fee of $0.00
No override keyword: the compiler warns, and the base method runs

How does this code work?

Both variables hold the same Card object, yet pay returns $0.00 and card returns $3.78. Without override, C# picks the method from the variable’s declared type at compile time.

CS0108 means you hid a non-virtual method. CS0114 means you hid a virtual one, so the compiler suggests adding override. Both are warnings, so the build still succeeds, and that’s why this bug ships.

Method 1: Override a Virtual Method with virtual and override (Recommended)

Mark the base method virtual and each derived version override, with the same name, parameters and return type. Same checkout, now with all three payment types in a List<Payment> in C#:

List<Payment> checkout = [new Card(), new Ach(), new PayPal()];
foreach (Payment p in checkout)
    Console.WriteLine($"{p.GetType().Name,-7} ${p.Fee(120m):0.00}");

class Payment { public virtual decimal Fee(decimal a) => 0m; }
class Card : Payment
{
    public override decimal Fee(decimal a) => a * 0.029m + 0.30m;
}
class Ach : Payment
{
    public override decimal Fee(decimal a) => 0.80m;
}
class PayPal : Payment
{
    public override decimal Fee(decimal a) => Math.Round(a * 0.0349m + 0.49m, 2);
}

Output:

Card    $3.78
Ach     $0.80
PayPal  $4.68
Method overriding in C# with virtual and override: a List<Payment> prints the Card, Ach and PayPal fees on a $120.00 order in VS Code
One loop, three overrides, chosen at runtime

How does this code work?

The loop variable is typed Payment, but each call runs the override of the real object: $3.78, $0.80 and $4.68. The runtime looks up the object’s type when the call happens, which is runtime polymorphism.

That’s the payoff in object-oriented programming in C#: the checkout code never asks which payment it holds, so adding a fourth type means adding one class.

Method 2: Call the Base Method from an Override with base

Sometimes you want to extend the base behavior, not replace it. Inside an override, base.Receipt(amount) runs the parent’s version. Same $120.00 order, now with a receipt line:

Payment p = new Card("Tyler Brooks", "4242");
Console.WriteLine(p.Receipt(120m));

class Payment(string customer)
{
    public virtual string Receipt(decimal amount) =>
        $"{customer} paid ${amount:0.00}";
}
class Card(string customer, string last4) : Payment(customer)
{
    public override string Receipt(decimal amount) =>
        base.Receipt(amount) + $" with the card ending {last4}";
}

Output:

Tyler Brooks paid $120.00 with the card ending 4242

How does this code work?

The Card override builds on the base text and appends the last four digits. The primary constructor passes customer up with Payment(customer), the same chaining my constructor in C# guide covers with base(...).

Note: base always calls the direct parent’s version, even through a Payment variable. That’s how you keep shared logic in one place.

Method 3: Override an Abstract Method

If the base class has no sensible default, make the method abstract. It has no body, and every non-abstract subclass must override it. Same Card and Ach fees here:

Payment[] methods = [new Card(), new Ach()];
foreach (Payment m in methods) Console.WriteLine(m.Label());

abstract class Payment
{
    public abstract decimal Fee(decimal amount);   // no body
    public string Label() => $"{GetType().Name}: ${Fee(120m):0.00}";
}
class Card : Payment
{
    public override decimal Fee(decimal amount) => amount * 0.029m + 0.30m;
}
class Ach : Payment
{
    public override decimal Fee(decimal amount) => 0.80m;
}

Output:

Card: $3.78
Ach: $0.80

How does this code work?

Label() is written once and calls Fee(), which only the subclasses implement, so it prints $3.78 and $0.80. Forget the override and the build fails with CS0534.

An abstract method is implicitly virtual, so the override syntax is identical. My abstract class in C# build-along goes deeper into abstract properties and constructors.

Method 4: Override a Property in C#

Properties, indexers and events can be virtual too. Here PayPal overrides three read-only properties instead of a method:

Payment p = new PayPal();
Console.WriteLine($"{p.Name}: {p.Rate * 100:0.00}% + ${p.FixedFee:0.00}");

class Payment
{
    public virtual string Name => "Payment";
    public virtual decimal Rate => 0m;
    public virtual decimal FixedFee => 0m;
}
class PayPal : Payment
{
    public override string Name => "PayPal";
    public override decimal Rate => 0.0349m;
    public override decimal FixedFee => 0.49m;
}

Output:

PayPal: 3.49% + $0.49

How does this code work?

Each => property is a getter, and the overrides replace them: 3.49% plus $0.49, the same rates in the sample table. Microsoft Learn’s rule: the overriding property must keep the same name, type and access modifier.

Method 5: Override ToString, Equals and GetHashCode

Every class inherits ToString(), Equals() and GetHashCode() from System.Object, and all three are virtual. Two copies of the same saved card show why you’d override them:

var a = new Card("Tyler Brooks", "4242");
var b = new Card("Tyler Brooks", "4242");
Console.WriteLine(a);
Console.WriteLine($"a == b: {a == b}");
Console.WriteLine($"a.Equals(b): {a.Equals(b)}");
Console.WriteLine($"Same hash: {a.GetHashCode() == b.GetHashCode()}");
Console.WriteLine($"Cards in HashSet: {new HashSet<Card> { a, b }.Count}");

class Card(string holder, string last4)
{
    public string Holder => holder;
    public string Last4 => last4;
    public override string ToString() => $"{holder} (card ending {last4})";
    public override bool Equals(object? obj) =>
        obj is Card c && c.Holder == Holder && c.Last4 == Last4;
    public override int GetHashCode() => HashCode.Combine(Holder, Last4);
}

Output:

Tyler Brooks (card ending 4242)
a == b: False
a.Equals(b): True
Same hash: True
Cards in HashSet: 1
C# override ToString, Equals and GetHashCode on a Card class: two cards for Tyler Brooks are equal and the HashSet keeps one
Value equality for two copies of the same card

How does this code work?

Console.WriteLine(a) calls the overridden ToString(). Equals compares holder and last four digits, so it’s True, while == still compares references and says False.

Important: override GetHashCode() whenever you override Equals. The HashSet kept 1 card because both hashes matched. For format strings and culture, see my guide to ToString() in C#.

Override vs new in C#: Which Method Runs?

The new modifier silences CS0108 and CS0114, but it doesn’t make the method polymorphic. Same Card object, one method overridden and one hidden:

Payment asBase = new Card();
Card asCard = new Card();
Console.WriteLine($"Fee():   via Payment = {asBase.Fee()}, via Card = {asCard.Fee()}");
Console.WriteLine($"Label(): via Payment = {asBase.Label()}, via Card = {asCard.Label()}");

class Payment
{
    public virtual string Fee() => "base fee";
    public virtual string Label() => "base label";
}
class Card : Payment
{
    public override string Fee() => "card fee";   // override: replaces
    public new string Label() => "card label";    // new: hides
}

Output:

Fee():   via Payment = card fee, via Card = card fee
Label(): via Payment = base label, via Card = card label
C# override vs new: through a Payment variable the overridden Fee returns card fee, while the hidden Label returns base label
override replaces the base method; new only hides it

How does this code work?

Fee() returns “card fee” through both variables. Label() returns “base label” through Payment and “card label” only through Card.

overridenew (hiding)
Base method must bevirtual, abstract or overrideAny accessible member
Call through a base variableRuns the derived methodRuns the base method
DecidedAt runtime (object type)At compile time (variable type)
Typical usePolymorphismA base class you don’t own added a clashing name

Pro Tip: treat new on a method as a red flag in code review. I only use it when a library’s base class adds a member with my method’s name.

Stop Further Overriding with sealed override

Add sealed to an override and classes further down can’t override it again. Same card fee, with a BusinessCard that inherits it:

Payment p = new BusinessCard();
Console.WriteLine($"{p.GetType().Name}: ${p.Fee(120m):0.00}");

class Payment
{
    public virtual decimal Fee(decimal amount) => 0m;
}
class Card : Payment
{
    public sealed override decimal Fee(decimal amount) => amount * 0.029m + 0.30m;
}
class BusinessCard : Card { }   // inherits Card.Fee and can't override it

Output:

BusinessCard: $3.78

How does this code work?

BusinessCard still gets $3.78 from Card.Fee(). Try to write override in BusinessCard and the compiler stops you with CS0239. Sealing a method you’ve finished is good encapsulation in C#.

Covariant Return Types in Method Overriding (C# 9)

Before C# 9, an override had to return exactly the base type, so Clone() gave you a Payment you had to cast. Now the override may return a more derived type:

Card card = new Card { Last4 = "4242" };
Card copy = card.Clone();          // no cast needed (C# 9+)
Console.WriteLine($"{copy.GetType().Name} ending {copy.Last4}");

class Payment
{
    public virtual Payment Clone() => new Payment();
}
class Card : Payment
{
    public string Last4 { get; init; } = "";
    public override Card Clone() => new Card { Last4 = Last4 };
}

Output:

Card ending 4242

How does this code work?

Card.Clone() returns Card, so copy.Last4 works without a cast and prints “Card ending 4242”. It works for methods and read-only properties, and it needs .NET 5 or later.

Method Overriding Rules and Compiler Errors in C#

Here are the two mistakes I see most, in one file. The build stops with both:

Console.WriteLine("This file never builds");

class Payment
{
    public decimal Fee(decimal a) => 0m;
    protected virtual string Code() => "PAY";
}
class Card : Payment
{
    public override decimal Fee(decimal a) => 1m;
    public override string Code() => "CARD";
}

Build output:

C:\DotNetDemo\OverrideDemo\Rules.cs(10,29): error CS0506: 'Card.Fee(decimal)': cannot override inherited member 'Payment.Fee(decimal)' because it is not marked virtual, abstract, or override
C:\DotNetDemo\OverrideDemo\Rules.cs(11,28): error CS0507: 'Card.Code()': cannot change access modifiers when overriding 'protected' inherited member 'Payment.Code()'

The build failed. Fix the build errors and run again.
Method overriding errors in C# in VS Code: CS0506 cannot override a non-virtual method and CS0507 cannot change access modifiers when overriding
Two broken overrides, two compiler errors
ErrorWhat you wroteFix
CS0506override on a method that isn’t virtualAdd virtual to the base method
CS0507public override of a protected methodKeep the same access modifier
CS0508A different return typeMatch it (or return a derived type, C# 9)
CS0112static virtual or static override in a classStatic methods can’t be overridden
CS0115override with no matching base methodCheck the name and parameters
CS0239override of a sealed overrideRemove sealed, or don’t override

The other rows came from separate test files, along with two more: virtual override together gives CS0113, and a private override gives CS0621. Interfaces work differently, since a class implements an interface in C# member without the override keyword.

Method Overloading vs Method Overriding in C#

The names sound alike, so interviewers love this one. Here’s the short version:

OverloadingOverriding
SignatureSame name, different parametersSame name, parameters and return type
WhereUsually the same classA derived class
KeywordsNonevirtual / abstract + override
ResolvedCompile timeRuntime

So overloading gives one class several versions of a method, and overriding swaps one version for another down the class hierarchy.

Things to Keep in Mind

  • Treat CS0108 and CS0114 as bugs: add override (and virtual on the base) unless you really mean to hide.
  • Keep the access modifier identical; a protected base method stays protected in every override.
  • Call base.Method() when you extend behavior, and skip it when you replace it.
  • Override GetHashCode() with Equals(), or dictionaries and sets will miss matches.
  • Mark a finished override sealed so classes further down can’t change it again (CS0239).

Overriding touches most of C# class design, so here are four related reads:

Frequently Asked Questions

What is method overriding in C#?

It’s when a derived class replaces a virtual or abstract base-class method with its own version marked override. Calls through a base-class variable then run the derived method, decided at runtime.

Why is my overridden method not being called in C#?

Usually the base method isn’t virtual, or the derived method is missing override. Then you’re hiding the method, the compiler warns with CS0108 or CS0114, and a base-class variable runs the base version.

What is the difference between override and new in C#?

override replaces the base method, so the object’s real type decides which one runs. new hides it, so the variable’s declared type decides.

Can you override a static or private method in C#?

No. Static methods can’t be virtual (CS0112), and a private method isn’t visible to derived classes, so there’s nothing to override.

Can a constructor be overridden in C#?

No. Constructors aren’t inherited, so they can’t be virtual. A derived constructor calls the base one with base(...) instead.

Where are the official rules for override?

Microsoft’s override modifier reference lists every rule, including covariant return types.