Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is "public new virtual void Method()" mean?

Tags:

c#

when use new virtual key words to decorate the method? what is the affection? Like define an interface, and add a class to inherit the interface. but use the new virtual to realize the interface method.

 interface IPrinter
{
    void Print();
}

 public class PrinterOne : IPrinter
{
    public void Print() 
    {
        Console.WriteLine("PrinterOne.");
    }
}

public class PrinterTwo : PrinterOne
{
    public new virtual void Print()
    {
        Console.WriteLine("PrinterTwo.");
    }
}

public class PrinterThree : PrinterTwo
{
    public override void Print()
    {
        Console.WriteLine("PrinterThree.");
    }
}

public class PrinterFour : PrinterThree
{
    public override void Print()
    {
        Console.WriteLine("PrinterFour.");
    }
}

 static void Main(string[] args)
    {
        IPrinter iprinter = new PrinterFour();
        iprinter.Print();//the output is PrinterOne? why???
        Console.ReadLine();
    }
like image 462
Laura_Li Avatar asked May 02 '12 14:05

Laura_Li


People also ask

What does public virtual mean C#?

The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it: C# Copy. public virtual double Area() { return x * y; }

What does a virtual method do?

A virtual method is a declared class method that allows overriding by a method with the same derived class signature. Virtual methods are tools used to implement the polymorphism feature of an object-oriented language, such as C#.

Why we declare a method as virtual?

When a method is declared as a virtual method in a base class then that method can be defined in a base class and it is optional for the derived class to override that method. The overriding method also provides more than one form for a method. Hence it is also an example for polymorphism.

How do you create 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.


3 Answers

new and virtual are two (mostly-) unrelated keywords.

new means it shadows the base method.
virtual allows subclasses to override it.

Calling the method through the interface results in the base method being called, since the base method is not virtual and the derived classes don't explicitly re-implement the interface (which would cause the method to be re-mapped)

like image 56
SLaks Avatar answered Oct 11 '22 15:10

SLaks


The new keyword used like this is member hiding.

I have never seen it used in conjunction with the virtual keyword, mind you. It is simply allowing types that derive from PrinterTwo to override the Print method implementation.

The new keyword used this way allows a type to hide the members of base types, but only if you are using a variable of the type itself.

For example, if you were to do:

PrinterOne one = new PrinterTwo();
one.Print();

It would not call the method in PrinterTwo as it is not part of the inheritance chain.

As for when you would do this... when you really, really need to for some odd reason that I can't think of (reflection maybe?) and you cannot edit the code in PrinterOne.

Personally, I wouldn't ever do this.

As for why the output is printer one... calling IPrinter.Print will call against the type it is defined on (PrinterOne in this case), which will put you back in my above example about the new keyword being ignored unless you talk to the type that features it.

Basically, using IPrinter is analogous to using PrinterOne in my small example above.

To solve the problem, make the PrinterOne method virtual and completely remove the use of new virtual in PrinterTwo.

like image 34
Adam Houldsworth Avatar answered Oct 11 '22 16:10

Adam Houldsworth


new modifier

http://msdn.microsoft.com/en-us/library/435f1dw2.aspx

When used as a modifier, the new keyword explicitly hides a member inherited from a base class.

This means that the method does not override the virtual base class method, but it still takes precedence when called on an instance of the derived class. In other words, the new method only affects a variable of the derived class, not the base class.

virtual modifier

http://msdn.microsoft.com/en-us/library/9fkccyh4.aspx

The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class.

This means that the method can be overriden in a derived class. When you call a virtual method on a base class variable which holds an instance of the derived class that has overridden the virtual method, the derived class implementation is called. This is the opposite of the behaviour of the new keyword.

like image 5
Kendall Frey Avatar answered Oct 11 '22 15:10

Kendall Frey