如何创建一个默认情况下所有字段/属性都是const的类?

时间:2012-06-06 15:19:52

标签: c# const

可以在C#中创建一个默认情况下所有字段和属性都是const的类吗?

  • 虚构的例子*

public class foo : const /* or readonly */ { public int baa = 3; }

1 个答案:

答案 0 :(得分:0)

不,不是。

您可以使类静态,但强制所有成员都是静态的。

例如:

public static class Constants
{
    public const int SomeId = 123;
    public const string SomeName = "foo";

    public static readonly DateTime myDateTime = new DateTime();

    public string notConstant = "test"; //does not compile
}