The multiple answers to question "Multi value Dictionary" propose to use an immutable class as TValue
in Dictionary<TKey, TValue>
Class.
The accepted Jon Skeet's answer proposes a class Pair
with readonly properties and @teedyay's answer to use the immutable Tuple
.
What is the rationale (or the possible benefits) of such approaches?
And collateral question:
Why to make TFirst and TSecond readonly if the respective properties First and Second do not have setters anyway:
private readonly TFirst first;
private readonly TSecond second;
public TFirst First
{
get { return first; }
}
public TSecond Second
{
get { return second; }
}
Update:
I am using dictionaries with custom classes for values in them.
And the va lues are being updated.
What are the possible reasons (benefits) for me to make them immutable?
I see that Lookup<TKey, TElement> Class
is also immutable and thought that I miss some benefits of using LINQ queries (?)
If so, can you give me examples what do I miss?
Basically, immutability makes various things easier to read about in my experience. For example, one of the big pain points in Java's Calendar
and Date
classes is their mutability. It's all to easy to forget they're mutable, take a copy of a reference in the constructor, and then find that something else mutates the object you've got a reference to. So you start taking a defensive copy even if nothing is going to change the object... it all gets very annoying.
There's a time for mutability, of course - but in many cases immutability is simply nicer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With