c#关键字用法:
- new operator Used to create objects on the heap and invoke constructors.
- new modifier Used to hide an inherited member from a base class member.
其中:
2)的new关键字只是让读者能够清楚的认识到这是覆盖父类的成员,在程序中没有什么价值,因为c#会自动的在子类中将父类的成员覆盖
例如:
public class Trade
{
public string Account { get; set; }
public void TradeMethod(string tradeId)
{
Console.WriteLine("This is Trade"+tradeId);
}
}
public class DriveTrade : Trade
{
public new void TradeMethod(string tradeId)
{
Console.WriteLine("This is DriveTrade"+tradeId);
}
}
在上面的DriveTrade中的方法TradeMethod的new关键字可以不用写,因为在实际运行中其实和没有new关键字是一样的