2

最近、OOPS についてブレインストーミングを行っているときに、突然 1 つの質問が頭に浮かびました。関連性があると思うので、コミュニティに質問することにしました。質問は次のとおりです。

静的コンストラクターは戻り値の型 (int、string など) を持つことはできませんが、C# では静的メソッドには戻り値の型が必要です。

c#コンパイラは、コンパイルを通過するために両方の状況をどのように区別しますか?

static Class staticClass
{
    public static staticClass(){} //right

    public static int staticClass(){} //wrong

    public static int staticMethod(){} //right
}
4

2 に答える 2

3

静的コンストラクターは戻り値の型 (int、string など) を持つことはできません

正しい。しかし、(静的) コンストラクターは何も返す必要はありません。

ただし、静的メソッドには C# の戻り値の型が必要です。

違う。静的メソッドは、void メソッドになる可能性が非常に高くなります。

C#コンパイラはどのように区別します...

static class StaticClass
{
  public static StaticClass(){} //right  : Wrong. 'public' is not allowed.

  public static int StaticClass(){} //wrong : Indeed wrong. Member cannot have same name as class

  public static int StaticMethod(){} //right
}
于 2013-07-11T19:19:11.533 に答える