Difference between new and override

后端 未结 14 1497
再見小時候
再見小時候 2020-11-22 05:36

Wondering what the difference is between the following:

Case 1: Base Class

public void DoIt();

Case 1: Inherited class

<         


        
相关标签:
14条回答
  • 2020-11-22 05:55

    All combinations of none, virtual, override, new and abstract:

    0 讨论(0)
  • 2020-11-22 05:58

    Out of all those, new is the most confusing. Through experimenting, the new keyword is like giving developers the option to override the inheriting class implementation with the base class implementation by explicitly defining the type. It is like thinking the other way around.

    In the example below, the result will return "Derived result" until the type is explicitly defined as BaseClass test, only then "Base result" will be returned.

    class Program
    {
        static void Main(string[] args)
        {
            var test = new DerivedClass();
            var result = test.DoSomething();
        }
    }
    
    class BaseClass
    {
        public virtual string DoSomething()
        {
            return "Base result";
        }
    }
    
    class DerivedClass : BaseClass
    {
        public new string DoSomething()
        {
            return "Derived result";
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:00

    In case 1 if you used call the DoIt() method of the inherited class while the type is declared as the base class you will see the action of the base class even.

    /* Results
    Class1
    Base1
    Class2
    Class2
    */
    public abstract class Base1
    {
        public void DoIt() { Console.WriteLine("Base1"); }
    }
    public  class Class1 : Base1 
    {
        public new void DoIt() { Console.WriteLine("Class1"); }
    }
    public abstract class Base2
    {
        public virtual void DoIt() { Console.WriteLine("Base2"); }
    }
    public class Class2 : Base2
    {
        public override void DoIt() { Console.WriteLine("Class2"); }
    }
    static void Main(string[] args)
    {
        var c1 = new Class1();
        c1.DoIt();
        ((Base1)c1).DoIt();
    
        var c2 = new Class2();
        c2.DoIt();
        ((Base2)c2).DoIt();
        Console.Read();
    }
    
    0 讨论(0)
  • 2020-11-22 06:03

    try following: (case1)

    ((BaseClass)(new InheritedClass())).DoIt()
    

    Edit: virtual+override are resolved at runtime (so override really overrides virtual methods), while new just create new method with the same name, and hides the old, it is resolved at compile time -> your compiler will call the method it 'sees'

    0 讨论(0)
  • 2020-11-22 06:04

    I had the same question and it's really confusing, you should consider that override and new keywords working only with objects of type base class and value of derived class. In this case only you'll see the effect of override and new: So if you have class A and B, B inherits from A, then you instantiate an object like this:

    A a = new B();
    

    Now on calling methods will take its state into consideration. Override: means that it extends the function of the method, then it uses the method in the derived class, whereas new tell the compiler to hide the method in the derived class and use the method in the base class instead. Here is a very good sight to that subject:

    https://msdn.microsoft.com/EN-US/library/ms173153%28v=VS.140,d=hv.2%29.aspx?f=255&MSPPError=-2147217396

    0 讨论(0)
  • 2020-11-22 06:09

    If keyword override is used in derive class then its override the parent method.

    If Keyword new is used in derive class then derive method hided by parent method.

    0 讨论(0)
提交回复
热议问题