In Java I can write:
public final static MyClass foo = new MyClass("foo");
Is there any equivalent in C#?
The closest thing (not exactly the same, final
has other meanings too) for Java final
fields I can think of is 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.
public const string MAGIC_STRING = "Foo";
sealed class finalClass
{
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.