【发布时间】:2010-11-13 23:35:48
【问题描述】:
在Java中我可以写:
public final static MyClass foo = new MyClass("foo");
在 C# 中是否有任何等价物?
【问题讨论】:
标签: c# java static final public
在Java中我可以写:
public final static MyClass foo = new MyClass("foo");
在 C# 中是否有任何等价物?
【问题讨论】:
标签: c# java static final public
我能想到的最接近 Java final 字段的东西(不完全相同,final has other meanings)是 readonly:
public static readonly MyClass field = new MyClass("foo");
如果您有原始类型(字符串、整数、布尔值),您可能希望使用 const。
public const string MAGIC_STRING = "Foo";
【讨论】:
final 字段就像 C# 中的 const 字段,但在这种情况下,我认为它们实际上是等效的。
readonly 和const 之间的区别。 const 烘焙值,所以如果你换出一个带有const 值的 DLL,其他使用该值的 DLL 将需要重建才能获得新值。 readonly 是运行时,不需要这个。
sealed class finalClass
{
...
}
【讨论】: