0
votes

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

2
You don't even need AnotherPerson here, you've just reassigned a reference, the original object was not mutated.Lucas Trzesniewski
You didn't mutate anything. You created another object with different data.Jeroen Vannevel
First: Immutability is more a "good practice" than a language construct. Second: you're re-assigning the variable "me". Even if you used a sealed class the results would be the same.Trauer
The DisplayName property is not immutable, the string object that it originally pointed to is immutableMathias R. Jessen
You might wanna check this link it proved helpful for me when i had my questions about immutable objects blogs.msdn.microsoft.com/ericlippert/2007/11/13/…Niklas

2 Answers

5
votes

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.

0
votes

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();
}