Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use case for C# allowing to use new on a virtual method?

Tags:

c#

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
like image 856
PeterR Avatar asked Sep 24 '09 16:09

PeterR


2 Answers

Use case:

  • Today, you use a 3rd party library and derive class Banana from class Fruit.
  • You implement a method called Peel in Banana. There is no Peel in Fruit.
  • Tomorrow, the 3rd party releases a new version of the library, including a virtual Fruit.Peel method
  • You recompile your code tomorrow. Do you want to override Fruit.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.

like image 101
Jon Skeet Avatar answered Oct 06 '22 00:10

Jon Skeet


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"

like image 43
Matt Avatar answered Oct 06 '22 00:10

Matt