3 ms·
Regarding immutability, C# 6 makes it a lot easier to write immutable types even for quick prototyping with get only properties: public string Example { get; }
by WorldMaker 11y ago
Regarding immutability, C# 6 makes it a lot easier to write immutable types even for quick prototyping with get only properties:
public string Example { get; }
You can set it in the constructor and only the constructor.
On the other hand in C# it's still relatively hard/annoying to build Structural Equality into your classes (which anonymous types and tuples both handle for you) where the equality of two objects is based on the contents of their properties rather than the equivalence of their references. (Ease of Structural Equality is the number one reason for my typical reliance on Tuple as I tend to use tuples often as Dictionary<K, V>/ILookup<K, V> keys.)
- nlawalker 11y agoThat C# 6 feature is nice; I had forgotten about that, thanks! Also, good call on structural equality, I hadn't even considered that and it's really valuable. One thing I've seen people do is create named tuples by inheriting from Tuple, which (I think) works but seems a little silly/ugly.
- WorldMaker 11y agoC# has also supported type aliases for a while: using MyTuple = Tuple<string, string, string>; But that's of limited utility (you have to include it in every code file, for instance) and I don't recall the exact circumstances but I recall it being brittle the last time I used it heavily (it was something like the compiler would fail to compile things that should be fine due to it forgetting a type alias half-way through a file), but that was also several compiler versions ago.