开发者

what the new mean in the following

In the interface, I saw the following

  public interface ITest : ITestBase
    {
        new string It开发者_C百科em { get; set; }
    }

I want to know the meaning of "new" here.


The new keyword in front of a property or method is used to hide the member with the same name in a parent class that is not virtual, and is considered by many (including me) bad practice because it may in many cases don't give you the result you expect.

An example:

class A{
  public int Test(){ return 1; }
}

class B : A{
  public new int Test(){ return 2; }
}

B b = new B();
Console.WriteLine( b.Test() );
A b2 = new B();
Console.WriteLine( b2.Test() );

This will print 2 and 1 respectively, and is confusing since both objects are in fact of type B.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜