After reading a lot about immutability in C # and understanding its advantages (no side effects, safe dictionary keys, multithreading ...), the following occurred to me:
Why is there no keyword in C # to say that a class (or structure) is immutable? This keyword should be checked at compile time so that you cannot mutate the class (or structure). For example:
public immutable class MyImmutableClass
{
public readonly string field;
public string field2;
public readonly AnyMutableType field3;
public string Prop { get; }
public string Prop2 { get; set; }
public AnyMutableType Prop3 { get; }
}
I think that working with the compiler will be quite simple, since only a few things will need to be checked:
- All public fields are read-only.
- All public properties have only recipients.
- All public fields or properties also have immutable types (simple value types or immutable classes / structures).
- ( /, , /, ). , , Equals(), GetHashCode() ToString().
:
- , / , , .
- (,
IEnumerable<T>) <T>. immutable , , IEnumerable<string> , .
, , , ?
?
?