简体   繁体   中英

Why are Immutable classes not Sealed in C#

I am asking the question regarding Immutable object pattern and implementing it. I am not talking about the existing classes in .Net library like String.

I understand that immutable objects are objects which once loaded cannot be modified by any external or internal component. What if I derive the immutable class as it is not a sealed class. Then assign the object to the base class, and call a method in the base class. I have effectively changed the state of the base immutable class as its state is that of the derived class object.

public class Person
{
    private readonly string name;

    public Person(string myName)
    {
        this.name = myName;
    }

    public string Name
    {
        get { return this.name; }
    }

    public void DisplayName()
    {
        Console.WriteLine(string.Format("Person's name is {0}", this.name));
    }
}

public class AnotherPerson : Person
{
    private string name1;

    public AnotherPerson (string myName) : base(myName)
    {
        this.name1 = myName;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person me = new Prasanth("MyName");
        me.DisplayName();
        me = new AnotherPerson("AnotherName"); ;
        me.DisplayName();
        Console.ReadLine();
    }
}

Output :

Person's name is MyName

Person's name is AnotherName

Let's forget about the flaws of your example (the comments already said it all) and answer your question: "why are Immutable classes not Sealed in C#."

The thing is, immutability isn't a feature of the C# language. Some languages support immutability as a feature (in which case your point would be valid), but C# doesn't. In the end, you're just building an immutable class out of existing, all-purpose features . And therefore, limitations can ensue.

Also, immutability is a precaution, not a protection . The point is to prevent anybody to change the data through "normal" means. If somebody really wants to change the data, they always can, for instance through reflection (or sub-classing, as you mentioned). But if a developer does that, then there's no way he's ignoring he's mutating data that is supposed to be read-only, and we can assume he has a good reason to do so. The point of immutability is to prevent the developer from unknowingly shooting himself in the foot, not to lock him down.

You can only assign readonly string name once. I'm currently not sure if this only possible in the constructor.

You assign it in the first run "MyName" and in the second run you assing "AnotherName" to a completly different object that you created with new AnotherPerson(...)

static void Main(string[] args)
{
    Person me = new Prasanth("MyName");
    me.DisplayName();
    // vvvvvv   here you lose the reference to the old object
    me = new AnotherPerson("AnotherName"); ;
    me.DisplayName();
    Console.ReadLine();
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM