This project is read-only.

readonly classes

Topics: C# Language Design
Apr 12, 2014 at 9:59 AM
Edited Apr 12, 2014 at 10:37 AM
I would suggest to add a readonly modifier for classes (at least for the sealed ones) and structs. Every field (including static and implicit fields) in such type must have a readonly modifier.
Classes inheriting readonly classes must be readonly themselves.
Example:
public readonly class A
{
    // Done right:
    public readonly int MyField;
    public static readonly int MyStaticField;

    public static int MyUnsecureField;         // COMPILE-TIME ERROR.
    public int MyUnsecureProperty {get;set;}   // COMPILE-TIME ERROR.
    public event EventHandler MyUnsecureEvent; // COMPILE-TIME ERROR.
}

public readonly class B : A {...}

// ERROR:
public class C : A {...}
It needs no new keywords, works just like static modifier for classes and improves reliability in a functional way.