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.
精彩评论