3 ms·
You are right about the first example (although the F# version looks nicer =D) But that struct example is less than great. Just last night I had a readonly C#
by algorithmsRcool 8y ago
You are right about the first example (although the F# version looks nicer =D)
But that struct example is less than great.
Just last night I had a readonly C# struct like that, that I used as a dictionary key and wondered why my perf tests took a nose dive when the Dictionary hit a few dozen entries.
My boneheaded mistake was i forgot to override GetHashCode and Object.Equals. Great my perf was looking decent, but even then I was allocating 32 bytes of memory on every lookup.
I forgot I needed to also implement IEquatable<> to prevent boxing on calls to Object.Equals.
My little 2 item struct is 3 lines of definition and 7 lines of boiler plate to get it right. and my GetHashCode impl is suboptimal.
F# records do all of that for you. every time. automatically.
- cgrealy 8y agoI 100% agree that F# does this better. but saying "unless someone knows C# very well one could have easily gotten this wrong" is disingenuous at best.
- algorithmsRcool 8y agoDustin is definitely giving a hard sell here I conceed