Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real life usage of new keyword to hide virtual method implementation? c#

What's the real life scenario where we will use new to provide new implementation for a virtual method in the derived class? C#

I know what it means technically. what I am looking for is a real life scenario where this will be needed.

We can always achieve the same by providing the override functionality. Why would we want to incorrect method been picked when methods call is made casted to base.

like image 280
ParvindS Avatar asked Jul 29 '11 06:07

ParvindS


People also ask

What is the use of new keyword in method hiding in C#?

It is also known as Method Shadowing. In method hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.

Which keyword is used to override a virtual method?

A virtual method is first created in a base class and then it is overridden in the derived class. A virtual method can be created in the base class by using the “virtual” keyword and the same method can be overridden in the derived class by using the “override” keyword.

What is the use of virtual keyword?

The virtual keyword is used to modify a method, property, indexer, or event declared in the base class and allow it to be overridden in the derived class. The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into a derived class.

Is it necessary to override a virtual method in C#?

When the method is declared as virtual in a base class, and the same definition exists in a derived class, there is no need for override, but a different definition will only work if the method is overridden in the derived class. Two important rules: By default, methods are non-virtual, and they cannot be overridden.


1 Answers

No. You can't achieve the same.

// Define the base class
class Car
{
    public virtual void DescribeCar()
    {
        System.Console.WriteLine("Four wheels and an engine.");
    }
}

// Define the derived classes
class ConvertibleCar : Car
{
    public new virtual void DescribeCar()
    {
        base.DescribeCar();
        System.Console.WriteLine("A roof that opens up.");
    }
}

class Minivan : Car
{
    public override void DescribeCar()
    {
        base.DescribeCar();
        System.Console.WriteLine("Carries seven people.");
    }
}

Now, if you try to do this:

public static void TestCars2()
{
    Car[] cars = new Car[3];
    cars[0] = new Car();
    cars[1] = new ConvertibleCar();
    cars[2] = new Minivan();
}

The result will be:

Car object: YourApplication.Car

Four wheels and an engine.

----------

Car object: YourApplication.ConvertibleCar

Four wheels and an engine.

----------

Car object: YourApplication.Minivan

Four wheels and an engine.

Carries seven people.

----------

Override ALWAYS override while new ONLY does it when is used as its declared type (not base one).

You can see more here

like image 92
Erre Efe Avatar answered Sep 23 '22 21:09

Erre Efe