In C#, what is the difference between override and new on a derived Send method, and which Send runs through a base-typed variable?
answer
- who decides: compiler or runtime
- same member vs a second member
- the variable's declared type
- CS0114 when neither keyword is written
basics
~20 soverride replaces the implementation of the base virtual Send, so a Notification-typed variable still runs the derived code. new only hides the name: through a Notification variable the base Send runs, through an EmailNotification variable the hiding one.
solid answer
~40 s`override` supplies a new implementation of the inherited `virtual` method, so the call is dispatched from the object's runtime type and the derived code runs whatever the variable's type. `new` declares a second, unrelated method that hides the inherited name: the compiler binds `n.Send()` from the static type of `n`, so a `Notification` variable reaches `Notification.Send` and only an `EmailNotification` variable reaches the hiding method. Writing neither keyword over a virtual member compiles with warning CS0114 and behaves as `new`. That is how a notification subclass silently stops sending email once a dispatcher handles it as `Notification`. The fix is `override`, with `base.Send()` if the shared base work must still run.
code
csharp · 21 linesNotification n = new EmailNotification();
EmailNotification e = (EmailNotification)n;
n.Send(); // through the base-typed variable
e.Send(); // through the derived-typed variable
Notification s = new SmsNotification();
s.Send();
class Notification
{
public virtual void Send() => Console.WriteLine("Notification.Send");
}
class EmailNotification : Notification
{
public new void Send() => Console.WriteLine("EmailNotification.Send");
}
class SmsNotification : Notification
{
public override void Send() => Console.WriteLine("SmsNotification.Send");
}go deeper
Recall the two outcomes: override runs the derived code through any variable type, new runs it only through a derived-typed variable. Be able to predict the output of the three-line example.
Explain why: override fills the inherited virtual method, while new is a second member found by compile-time name lookup on the static type. Name CS0114 and CS0108 and what each means.
Treat every CS0114 in a build as a latent dispatch bug, and explain the versioning reason C# asks for override or new when a base library adds a member your class already has.
Decide whether hiding warnings become build errors for the team, and where new is ever acceptable in a published type, given that callers holding the base type will never see it.
## The bug in one picture A notification system has a `Notification` base class with `public virtual void Send()`, and email, SMS and push subclasses. A teammate wrote `public new void Send()` in `EmailNotification` instead of `public override void Send()`. Everything looks fine in a unit test that does `new EmailNotification().Send()`, yet in production, where the dispatcher holds a `List<Notification>`, no email is ever sent: the base implementation runs instead. ```csharp Notification n = new EmailNotification(); n.Send(); // Notification.Send ((EmailNotification)n).Send(); // EmailNotification.Send ``` The difference is **which side decides**: virtual dispatch is decided when the code runs, name hiding is decided when the caller is compiled. ## override: the same method, a new implementation - `virtual` on the base member opts it in to **run-time dispatch**. C# methods are non-virtual unless declared so. - `override` does not create a new method. It supplies a new **implementation of the inherited virtual method**, the same member the base class declared. - At run time a call `n.Send()` looks at the **runtime type** of the object that `n` refers to and runs the most derived override. The variable's declared type does not matter: a `Notification` variable and an `EmailNotification` variable reach the same override. - Code inside the base class that calls `Send()` on `this` reaches the override too. That is the whole point of a template method. ## new: a second method with the same name `new` declares a **separate member** that hides the inherited one. The base method still exists, unchanged, and still sits where virtual calls look for an implementation. 1. The compiler binds a call by looking up `Send` on the **static type** of the receiver expression. 2. If that type is `EmailNotification` (or a type derived from it that does not hide again), lookup finds the hiding method. 3. If that type is `Notification`, lookup finds the base virtual method, and dispatch then finds no override of it in `EmailNotification`, so `Notification.Send` runs. Three consequences surprise people: - The base class's own code never reaches the hiding method, because it was compiled against `Notification.Send`. - A generic method constrained with `where T : Notification` calling `item.Send()` also binds to `Notification.Send`, even when `T` is `EmailNotification`. - `new` methods are not virtual unless you write `new virtual`. A class deriving from `EmailNotification` that tries `override` on the hiding `Send` gets error **CS0506**; with `public new virtual void Send()` it can override, which starts a fresh virtual chain that `Notification`-typed calls still never reach. ## What the compiler says | You write in the derived class | Base member is | Result | |---|---|---| | `override void Send()` | `virtual`, `abstract` or `override` | Override; run-time dispatch reaches it | | `override void Send()` | non-virtual | Error CS0506 | | `new void Send()` | any accessible member | Hiding, no diagnostic | | `void Send()` (no keyword) | `virtual` | Warning **CS0114**, behaves as `new` | | `void Log()` (no keyword) | non-virtual | Warning **CS0108**, behaves as `new` | | `new void Ping()` | nothing to hide | Warning CS0109 | Leaving out the keyword is therefore **not an error**. CS0114 says the member "hides inherited member" and asks you to add `override` or `new`. A team that keeps warnings as warnings can ship the bug; `TreatWarningsAsErrors` turns it into a build break. ## Why C# makes you say which one you mean The rule exists for **versioning**. Suppose `EmailNotification` already had its own `Send` and a later release of the base library adds `virtual Send`. Code already compiled keeps calling the method it was compiled against, so nothing changes silently. When you recompile, the compiler reports CS0114 and treats your method as hiding until you decide: - `override` if your method really is the email implementation of the new base operation; - `new` if it is an unrelated method that happens to share the name; - a rename, if hiding would confuse readers. ## Fixing the notification bug - Replace `new` with `override` on `EmailNotification.Send`. Through every variable type the email implementation now runs. - If the base `Send` does shared work (logging, retries), call `base.Send()` from the override at the point where that work should happen. - Search for other CS0114 warnings in the build output; each one is a method that hides a virtual member, which is the same bug waiting for a base-typed caller. - Keep `new` for the rare case where a derived type deliberately exposes a different member under the same name, such as a more specific return type on a non-virtual member, and document it.
- Your class already has a Send method and a new release of the base library adds a virtual Send. What happens?Already-compiled code keeps calling the method it was compiled against, so nothing changes until you recompile. Then the compiler reports CS0114 (CS0108 if the new base member is not virtual) and treats your method as hiding. You choose: `override` if your method is the implementation of the new base operation, `new` if it is unrelated, or a rename. C# makes the choice explicit so a base-class addition never silently takes over your method.
- Can a class deriving from EmailNotification override a Send that EmailNotification declared with new?Only if the hiding method is itself virtual. `public new void Send()` is non-virtual, so `override` below it fails with CS0506. `public new virtual void Send()` starts a fresh virtual chain: overrides below it run through `EmailNotification`-typed variables, but a `Notification`-typed variable still reaches `Notification.Send`, because that is a different method.
- Which Send runs when a generic method with where T : Notification calls item.Send() on an EmailNotification?`Notification.Send`. Inside the generic method the compiler binds to the member it finds on the constraint type, `Notification`, and the call dispatches virtually from there. An override would be reached; a hiding `new` method is a different member and is never seen.
- Says new and override both make the derived Send run through a base reference.
- Believes leaving out override over a virtual method is a compile error rather than warning CS0114.
- Claims new hiding is resolved at run time from the object's actual type.
- Thinks C# methods are virtual by default, so override is just documentation.
- Expects base-class code calling Send to reach a hiding new method.