C# 使用带分号的基本构造函数

C# Using Base Constructors with Semicolon

在 C# 中创建引用继承 classes 的超级构造函数的构造函数时,为什么我需要使用大括号,而不是选择用分号终止语句?

想到这个问题,因为我目前有一个抽象 class,它很容易从执行的命令中继承 return 结果。

例如:

 abstract class CommandResult<T> {
    protected CommandResult() { }

    protected CommandResult(string msg) : this() {
        Message = msg;
    }

    protected CommandResult(string msg, T result) : this(msg) {
        Result = result;
    }

    protected CommandResult(T result) : this() {
        Result = result;
    }
 }

将是我的摘要 class。 在引用它的 class 中,它们目前看起来如下所示:

 public class CommandIntResult: CommandResult<int> {

    public CommandIntResult() { }

    public CommandIntResult(string msg) : base(msg) { }

    public CommandIntResult(string msg, int result) : base(msg, result) { }

    public CommandIntResult(int result) : base(result) { }

}

构造函数体中没有逻辑。 要求括号而不是用分号终止的选项背后的原因是什么,如下所示?

public CommandIntResult(string msg, int result): base(msg, result);

在方法声明的上下文中,; 并不意味着 "this is an empty method"。

abstract方法上,它表示"there is no method body"。

extern 方法上,这意味着实际代码位于其他地方。

对于 属性 getter 和 setter,表示 "the code for this method is automatically generated"。

如果你想写一个 empty 方法体,已经有一种方法可以写,而且它只长一个字符 - {}.