C# and method hiding

后端 未结 9 1185
悲&欢浪女
悲&欢浪女 2021-01-03 07:48

Per MSDN, the \"new\" keyword when used for method hiding only suppresses a warning.

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

Do I really need

相关标签:
9条回答
  • 2021-01-03 08:26

    As far as I know, the only difference is the one you suggest: new hides the warning. I tried out the following code:

    class Base
    {
        public void Test(){}
    }
    
    class ChildA : Base
    {
        public void Test(){}
    }
    
    
    class ChildB : Base
    {
        public new void Test(){}
    }
    

    The test method for both classes look identical in IL:

    .method public hidebysig instance void  Test() cil managed
    {
      // Code size       1 (0x1)
      .maxstack  8
      IL_0000:  ret
    } // end of method ChildA::Test
    
    .method public hidebysig instance void  Test() cil managed
    {
      // Code size       1 (0x1)
      .maxstack  8
      IL_0000:  ret
    } // end of method ChildB::Test
    

    The compiler issued a warning for Test in ClassA, but not in ClassB. But as other answers has stated; don't confuse the concepts of method hiding and method overriding; not the same thing.

    0 讨论(0)
  • 2021-01-03 08:31

    You get method hiding whether or not you specify "new", but it's not the same as overriding. Here's an example where they're different:

    using System;
    
    class Base
    {
        public virtual void OverrideMe()
        {
            Console.WriteLine("Base.OverrideMe");
        }
    
        public virtual void HideMe()
        {
            Console.WriteLine("Base.HideMe");
        }
    }
    
    class Derived : Base
    {
        public override void OverrideMe()
        {
            Console.WriteLine("Derived.OverrideMe");
        }
    
        public new void HideMe()
        {
            Console.WriteLine("Derived.HideMe");
        }
    }
    
    class Test
    {
        static void Main()
        {
            Base x = new Derived();
            x.OverrideMe();
            x.HideMe();
        }
    }
    

    The output is:

    Derived.OverrideMe
    Base.HideMe
    

    Even though the base HideMe method is virtual, it isn't overridden in Derived, it's just hidden - so the method is still bound to the virtual method in Base, and that's what gets executed.

    Member hiding is generally a bad idea, making the code harder to understand. The fact that it's available is beneficial in terms of versioning, however - it means adding a method to a base class doesn't potentially let derived classes override it unintentionally, and they can keep working as before. That's why you get the warning.

    0 讨论(0)
  • 2021-01-03 08:40

    Use of new keyword while hiding a method is to "make the intent of the programmer clear". This avoids accidental hidings by the programmers.

    If the new keyword is not present then the compiler issues a warning and treats it as if it was present.

    You can read more about it Versioning with the Override and New Keywords (C# Programming Guide)

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