Being mainly a Java developer, I was a bit surprised by the result when I one day accidentally used new keyword instead of override.
It appears that the new keyword removes the "virtualness" of the method at that level in the inheritance tree, so that calling a method on an instance of child class that is downcasted to the parent class, will not resolve to the method implementation in the child class.
What are the practical use cases for this behavior?
Clarification: I understand the use of new when parent is not virtual. I'm more curious why the compiler allows new and virtual to be combined.
The following example illustrates the difference:
using System;
public class FooBar
{
public virtual void AAA()
{
Console.WriteLine("FooBar:AAA");
}
public virtual void CCC()
{
Console.WriteLine("FooBar:CCC");
}
}
public class Bar : FooBar
{
public new void AAA()
{
Console.WriteLine("Bar:AAA");
}
public override void CCC()
{
Console.WriteLine("Bar:CCC");
}
}
public class TestClass
{
public static void Main()
{
FooBar a = new Bar();
Bar b = new Bar();
Console.WriteLine("Calling FooBar:AAA");
a.AAA();
Console.WriteLine("Calling FooBar:CCC");
a.CCC();
Console.WriteLine("Calling Bar:AAA");
b.AAA();
Console.WriteLine("Calling Bar:CCC");
b.CCC();
Console.ReadLine();
}
}
This produces the following output:
Calling FooBar:AAA
FooBar:AAA
Calling FooBar:CCC
Bar:CCC
Calling Bar:AAA
Bar:AAA
Calling Bar:CCC
Bar:CCC
Use case:
Banana
from class Fruit
.Peel
in Banana
. There is no Peel
in Fruit
.Fruit.Peel
methodFruit.Peel
? Quite possibly not - it could have a completely different meaning. Instead, you hide it with Banana.Peel
and all the existing code works as it does today.In other words, it's mostly to avoid versioning issues. In Java, you'd end up overriding Fruit.peel
even though you didn't want to, quite possibly leading to hard-to-diagnose bugs.
Speaking from personal experience, I mostly see the "new" keyword used in cases where the original parent method was not specified as virtual, but an override behavior was desired. Application of the "new" keyword "hides" the parent method. And, as you observed in the code example, the method written with "new" will only be executed when working directly with that type. If working with the parent type, the parents original method will be called.
To answer your question more directly - it provides a means of overriding methods when the parent method was not labeled virtual.
EDIT: As an aside, adding the "new" keyword to hide a parent method that is not naturally overrideable does not actually change anything in the generated IL. But it is a means of explicitly stating to the developer "Hey, you're hiding a parent method here, not overriding it"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With