views:

406

answers:

2

In Java I can write:

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

is there an equivalent in C#?

+4  A: 

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");
Mehrdad Afshari
For fields, they're *almost* exactly the same thing. There are different rules around initialization (irrelevant here) and for some times `final` fields in Java act like `const` fields in C#, but in this case I think they're effectively equivalent.
Jon Skeet
Many thanks - this is what I needed
peter.murray.rust
note that for primitive types there's also "const".
Robert Fraser
The criteria for `const` is not being primitive or not, rather, it's being able to literally describe a value for it in code. You can have *any* `const` with a reference type set to `null`.
Mehrdad Afshari
A: 
sealed class finalClass
{
   ...
}
Mike
That's for a *class* - this is a *field*.
Jon Skeet
my bad, so it is
Mike