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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:13:38  来源:igfitidea点击:

What is the c# equivalent of public final static in java

c#javastaticfinalpublic

提问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, finalhas other meaningstoo) for Java finalfields I can think of is readonly:

我能想到的最接近的Java字段(不完全相同,final也有其他含义finalreadonly

public static readonly MyClass field = new MyClass("foo");

If you have a primitive type (string, int, boolean), you may want to use constinstead.

如果你有一个原始类型(字符串、整数、布尔值),你可能想const改用。

public const string MAGIC_STRING = "Foo";

回答by Steve

sealed class finalClass
{
   ...
}