[英]When inheriting from a framework control, should I call the base constructor?
[英]Do I call the base constructor when deriving from ServiceBase?
System.ServiceProcess.ServiceBase类构造函数的Microsoft .NET文档说:
如果重写基类构造函数,则应在派生类的构造函数中显式调用它。
在“Microsoft C#编程指南”中的“ 使用构造函数 ”中,它说:
在派生类中,如果未使用base关键字显式调用基类构造函数,则会隐式调用默认构造函数(如果有)。
所以我需要明确地调用基本构造函数,为什么?
没关系。 这些编译成相同的IL:
public class Service1 : ServiceBase
{
public Service1() : base() { }
}
public class Service1 : ServiceBase
{
public Service1() { }
}
我不知道为什么ServiceBase
建议显式调用它。 我会选择其他建议,因为它似乎更有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.