In other words, what's the default (if nothing is specified)? I'm guessing the default is virtual, because you can use the "new" keyword to override a base method even when the base method has no virtual specified. If that's the case then why do we even need a Virtual option? Couldn't we just use Sealed when we do need to prevent further inheritance?
Asked
Active
Viewed 238 times
2 Answers
10
C# methods are sealed by default -- you cannot override them without the virtual
keyword.
The new
keyword hides the method in the base class.
Here's what I mean by hiding:
public class HidingA
{
public string Get()
{
return "A";
}
}
public class HidingB : HidingA
{
public new string Get()
{
return "B";
}
}
HidingA a = new HidingA();
HidingB b = new HidingB();
Console.WriteLine(a.Get()); // "A"
Console.WriteLine(b.Get()); // "B"
HidingA c = new HidingB();
Console.WriteLine(c.Get()); // "A". Since we're calling this instance of "B" an "A",
//we're using the "A" implementation.
Now, the Virtual version!
public class VirtualA
{
public virtual string Get()
{
return "A";
}
}
public class VirtualB : VirtualA
{
public override string Get()
{
return "B";
}
}
VirtualA a = new VirtualA();
VirtualB b = new VirtualB();
Console.WriteLine(a.Get()); // "A"
Console.WriteLine(b.Get()); // "B"
VirtualA c = new VirtualB();
Console.WriteLine(c.Get()); // "B". We overrode VirtualB.Get, so it's using the
// "B" implementation of the method
So if we make a method that takes HidingA
as a parameter and pass it an instance of a HidingB
, we're going to get the HidingA
implementation of the Get
method.
MSDN: http://msdn.microsoft.com/en-us/library/6fawty39.aspx
Classes are open for inheritance unless the sealed
keyword is specified.

Daniel Mann
- 57,011
- 13
- 100
- 120
-
1I'd include a mention of the difference between `new` and `override`, but this is the most accurate answer so far. – Magus Jun 10 '14 at 16:26
-
1
-
-
There's no such thing as a "virtual" class in C#, since multiple inheritance is not allowed. Unsealed classes can be subclassed, and virtual methods within a class can be overridden in subclasses. – Daniel Mann Jun 10 '14 at 16:34
-
Sorry for more questions, but I am trying to get an in-depth understanding. What is the connection between virtual and multiple inheritance? i.e. Why is virtual irrelevant in single inheritance? – Prabhu Jun 10 '14 at 16:38
-
Also, what does "hide" really mean? If a base method gets "hidden" with the use a of the "new" keyword, does that mean the base method is gone forever? Or is it only inaccessible within the sub class and further sub classes? – Prabhu Jun 10 '14 at 16:42
-
Consult your search engine of choice for an in-depth explanation of virtual base classes in languages that support multiple inheritance. – Daniel Mann Jun 10 '14 at 16:42
-
Ah ok, so "new" means that the method is a completely new method and not associated in anyway with the base method which happens to share the same name... – Prabhu Jun 10 '14 at 17:07
1
new
creates a new method (not overriding it), where as an overridden one would replace it, hence being virtual
. As stated above by DanielM, methods are sealed by default.
This is why we need virtual:
class Foo
{
public void Baz() { Assert("Foo.Baz"); }
}
class ChildOfFoo : Foo
{
public new void Baz() { Assert("ChildOfFoo.Baz"); }
}
Foo foo = new ChildOfFoo();
foo.Baz(); // Foo.Baz

Daniel A. White
- 187,200
- 47
- 362
- 445