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

C#JavaStaticFinalPublic

C# Problem Overview


In Java I can write:

public final static MyClass foo = new MyClass("foo");

Is there any equivalent in C#?

C# Solutions


Solution 1 - 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";

Solution 2 - C#

sealed class finalClass
{
   ...
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpeter.murray.rustView Question on Stackoverflow
Solution 1 - C#mmxView Answer on Stackoverflow
Solution 2 - C#SteveView Answer on Stackoverflow