3 ms·
Aren’t records pretty trivial to create as a custom class in C#?
by jimbob45 6y ago
Aren’t records pretty trivial to create as a custom class in C#?
- 4rt 6y agothey compile to classes in the background. the benefit is in the succinct syntax, value-type style equality checks and built in immutability (via the new init-only properties).
- smt88 6y agoAbsolutely not. Records generate a lot of code when you compile them, including methods to compare by value. The boilerplate required to do that in C# without records is extensive and hard to maintain. Even structs, which are supposed to be value objects, are not easy to compare by value. I'll put it this way: records are important enough that I stopped using C# because it lacked something like records, and now I'm going to pick it up again.
- Serow225 6y agoIf you want to see how much code you get 'for free' by defining a record that you used to have to do manually, check out the generated C# here (structural equality, GetHashCode, Deconstruct, cloning for 'with', pretty printing, etc) https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQBMQGoA+BYAUANwgQAIAzAe3OIF5iA7OAd2IAVEpy6AKAIgClycHkmIAmAAwBKANx48COAGNyCFK3adiXAAIBGccQByEMHBEBLOjGIBBAOZwZc3AHoX5u3RVxiMABbmUMSMKgDWROQArnRqFCRQfkQADgA2EMDEYIFQlnbEAMIAxACcxFCRSUkqMHh0JnBQSRCKPnoAdABK0TDmpm355GBJ5imIAMqI+OYtQQDeeMSLxNoAzMuixACSUACiAB7wCHUpm3Tm1rMAvniXQA== https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQBMQGoA+BYAUANwgQAI...
- Jordanpomeroy 6y agoThat’s neat! It’s also generating code for the main method, which is also a new c# 9 feature
- Renaud 6y agoAren't records meant to be immutable? I don't understand why properties have setters in the generated definition of the record. I'm missing something.
- Todd 6y agoConstructor-defined properties of records are implemented using auto-implemented properties with the new init keyword, so: public int MyProp { get; init; } instead of public int MyProp { get; set; } Apparently those are implemented using readonly backing fields while still retaining property setters.
- BatteryMountain 6y agoThing is, you can do it but with ceremony. You have to override Equals() and GetHashCode() for starters. Then you also need to make sure you only have getters and no setters (or private setters). In some cases you need to make your empty constructor private and force a specific constructor to be used. If you have multiple constructors it means you will have different invariants which puts you in class land, you are better off with classes then anyway. Also if your immutable objects have many methods..that also puts you in class land. Just that alone makes it cumbersome. So a new record type that behaves somewhat like a struct would be perfect. Or the F# way, which is basically perfection.
- BatteryMountain 6y agoI also want to stress that this is per class. Which in reality is madness. If you have just 50 immutable classes you have to replicate those 50 lines of boilerplate for each class and make sure it's bug free. It clutters your classes up and it's just plain gross. It's going to be way better that the platform supports this kind of data structure natively instead of us building out the monstrosity ourselves. By the way, plenty of the features of C# is basically syntax sugar, gets compiled down to more vebose code anyway. We think we write one or two lines of happy code, but underneath it might get turned into 20 lines. Luckily we mostly don't have to see/deal with it unless you are trying to squeeze your apps performance to the max (or have a nasty bug that cannot be caught by normal means), but at that point it might make more sense to use a lower level language.
- thomasz 6y agoThere is no correlation between lines of code and performance. Longer code is often faster, while short, succinct code is often slower.