views:

391

answers:

6

Why should virtual methods be explicitly overridden in C#?

+5  A: 

Because it makes code more readable:

class Derived : Base
{
    void Foo();
}

In C++, Foo may or may not be a virtual method, we can't tell by looking at the definition. In C# we know a method is virtual (or isn't) because there is either a virtual or override keyword.

Jason's comment below is a better answer.

(edited for clarity)

Tergiver
... and it gives Intellisense the ability to help us override a method. Type "override" and Intellisense pops up with a choice, and even fills in the entire method signature for us. How awesome is that?
Tergiver
...and, most importantly, it allows the compiler to tell us when we make a mistake (e.g. if you add a parameter to the base class method in C++, it breaks all the derived classes but you have no way of knowing it - a cause of some really nasty to trace bugs, because bits of the derived class behaviour just quietly stop working. In C# it gives an error for every override that no longer overrides anything)
Jason Williams
_"In C++ it might be, we can't tell by looking at the definition."_ -- By looking at the definition, we can tell that it's not C++ at all.
stakx
@stakx: That whooshing sound you heard was you missing the point.
R. Bemrose
I think I *do* get the point, _now that I know that there is one._ All in all, I find this answer more confusing than helpful, though. (-1)
stakx
I say this answer is wrong, as adding `override` makes the code behave quite different.
Dykam
@stakx: You're right, I could have written it in C++, which would have been clearer@Dykam: C# does not allow you to override without the keyword. If there is no override keyword, there cannot be a method with that signature to override. There is no "different behavior" because they are two different things (overridding, not overridding).
Tergiver
+14  A: 

By declaring a method as virtual, you are stating your intention that the method can be overridden in a derived class.

By declaring your implementing method as override, your are stating your intention that you are overriding a virtual method.

By requiring that the override keyword be used to override a virtual method, the designers of the language encourage clarity, by requiring you to state your intentions.

Robert Harvey
Why not mark it `abstract` and force overriding?
George Stocker
@George: Because you can only use the abstract modifier in abstract classes.
Jamie Ide
@George Stocker - Because you might not want to mark your entire class as abstract (the class needs the abstract modifier to have abstract methods).
Justin Niessner
The `override` keyword is not required – see my answer.
SLaks
@SLaks: It *is* if you want to override.
Robert Harvey
This answer does not mention the default behavior `new` at all. That is, when you are not overriding, you are hiding the base implementation.
Dykam
(Copied my comment up here, as it's an important point. Should have made it an answer I guess :-) ...override also allows the compiler to tell us when we make a mistake (e.g. if you add a parameter to the base class method in C++, it breaks all the derived classes but you have no way of knowing it - a cause of some really nasty to trace bugs, because bits of the derived class behaviour just quietly stop working. In C# it gives an error for every override that no longer overrides anything)
Jason Williams
+8  A: 

If you don't add the override keyword, the method will be hidden (as if it had the new kwyword), not overridden.

For example:

class Base {
    public virtual void T() { Console.WriteLine("Base"); }
}
class Derived : Base {
    public void T() { Console.WriteLine("Derived"); }
}

Base d = new Derived();
d.T();

This code prints Base. If you add override to the Derived implementation, the code will print Derived.

You cannot do this in C++ with a virtual method. (There is no way to hide a C++ virtual method without overriding it)

SLaks
Why do so many people do not know about the default `new` behaviour...
Dykam
I agree that this should not be legal and should throw a Error instead of a Warning. (Edit: Someone removed his comment above this one).
Dykam
Warings as errors FTW!
Wim Coenen
+1  A: 

Not all virtual methods should be overridden, though all abstract methods should (and must) be. As for why the 'override' keyword is explicit, that's because overriding and hiding behave differently. A hiding method is not called through a reference to a base class, whereas an overridden method is. This is why the compiler specifically warns about how you should use the 'new' keyword in the case where you are hiding rather than overriding.

Dan Bryant
+4  A: 

It is because the C# team members are all skilled C++ programmers. And know how incipient this particular bug is:

class Base {
protected:
    virtual void Mumble(int arg) {}
};

class Derived : public Base {
protected:
    // Override base class method
    void Mumble(long arg) {}
};

It is a lot more common then you might think. The derived class is always declared in another source code file. You don't typically get this wrong right away, it happens when you refactor. No peep from the compiler, the code runs pretty normal, just doesn't do what you expect it to do. You can look at it for an hour or a day and not see the bug.

This can never happen in a C# program. Even managed C++ adopted this syntax, breaking with native C++ syntax intentionally. Always a courageous choice. IntelliSense takes the sting out the extra verbiage.

There's a lot of syntax tweaks in C# that resemble this kind of bug avoidance syntax.

Hans Passant
A: 

There is no need to explicitly override a virtual method in a derived class. Marking a method virtual only enables overriding.

Ishmael