java中public final static的c#等价物是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1181753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the c# equivalent of public final static in java
提问by peter.murray.rust
In Java I can write:
在 Java 中,我可以写:
public final static MyClass foo = new MyClass("foo");
Is there any equivalent in C#?
C# 中是否有任何等价物?
采纳答案by Mehrdad Afshari
The closest thing (not exactly the same, final
has other meaningstoo) for Java final
fields I can think of is readonly
:
我能想到的最接近的Java字段(不完全相同,final
也有其他含义)final
是readonly
:
public static readonly MyClass field = new MyClass("foo");
If you have a primitive type (string, int, boolean), you may want to use const
instead.
如果你有一个原始类型(字符串、整数、布尔值),你可能想const
改用。
public const string MAGIC_STRING = "Foo";
回答by Steve
sealed class finalClass
{
...
}