Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReadonlyCollection, are the objects immutable?

I'm trying using ReadOnlyCollection to make object immutable, I want the property of object are immutable.

public ReadOnlyCollection<FooObject> MyReadOnlyList
{
    get
    {
        return new ReadOnlyCollection<FooObject>(_myDataList);
    }
}

But I little confused.

I tried to change the property of the object in to MyReadOnlyList using a foreach and ... I can change value property, is it correct? I understood ReadOnlyCollection set an add level to make the object immutable.

like image 492
lunatic84 Avatar asked Sep 07 '15 12:09

lunatic84


People also ask

Is IReadOnlyList immutable?

An immutable object is defined as an object that cannot be changed after it has been created. Not all collections are immutable, but you can use the read-only collection types in . NET Core such as IReadOnlyList, IReadOnlyDictionary, and IReadOnlyCollection to implement immutable types.

Is a read only property immutable?

Because value types directly contain their data, a field that is a readonly value type is immutable.

What are immutable types in C#?

An immutable type, in the context of C#, is a type of object whose data cannot be changed after its creation. An immutable type sets the property or state of the object as read only because it cannot be modified after it is assigned during initialization.

Is readonly immutable C#?

If the field is a value type, marking it readonly makes it immutable. On the other hand, if a readonly field is a reference type, then you can still change the data of the object referenced by the variable. However, you can't change that reference to point to a new object.


1 Answers

The fact that ReadOnlyCollection is immutable means that the collection cannot be modified, i.e. no objects can be added or removed from the collection. This does not mean that the objects it contains immutable.

This article by Eric Lippert, explains how different kinds of immutability work. Basically, a ReadOnlyCollection is an immutable facade which can read the underlying collection (_myDataList), but cannot modify it. However, you can still change the underlying collection since you have a reference to _myDataList by doing something like _myDataList[0] = null.

Furthermore, the objects returned by ReadOnlyCollection are the same ones returned by _myDataList, i.e. this._myDataList.First() == this.MyReadOnlyList.First() (with LINQ). This means that if an object in _myDataList is mutable, then so is the object in MyReadOnlyList.

If you want the objects to be immutable, you should design them accordingly. For instance, you might use:

public struct Point
{
    public Point(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }

    // In C#6, the "private set;" can be removed
    public int X { get; private set; }
    public int Y { get; private set; }
}

instead of:

public struct Point
{
    public int X { get; set; }
    public int Y { get; set; }
}

Edit: in this case, as noted by Ian Goldby, neither struct allows you to modify properties of the elements in the collection. This happens because structs are value types and when you access an element the collection returns a copy of the value. You can only modify the properties of a Point type if it is a class, which would mean that references to the actual objects are returned, instead of copies of their values.

like image 112
Fernando Matsumoto Avatar answered Sep 30 '22 18:09

Fernando Matsumoto