This is indeed a nice read, but I’m not sure it fully convinces me that record types are actually important (unless you intend to use with
). It sets up a problem, solves it with “conventional” C#, and then switches one keyword around to use record
without, imo, successfully motivating that switch.
The rest of the code is unchanged, instances of the record will act the same as if they were of class – except now we can take advantage of the other features of record.
If the only advantage is the new features of records, then the article should explain those features and why they are important.
I agree with you. It does a good job explaining a potential use case for record types. I also agree it doesn’t completely convince me in the example that record type is a significant improvement over the “conventional” C# method.
It’s more important of building a mindset of immutable by default. Record (case class) removes all boilerplate of having a read-only class.
It’s particularly useful if you are writing something common for a team, or a framework used by many other people, so you don’t worry about when you send something out, something may alter the values.
I don’t know why we couldn’t just get some of this syntax sugar for classes. We really didn’t need another type of data structure. I honestly only think the useful part of this is removing boilerplate for mock classes, the rest seems to focus far too much on exposing every property a class has.
I could do all of this with classes before, so why not add with
to classes or just allow us to mark classes as readonly
? That doesn’t involve a new keyword and also looks familiar to developers.
C# 9 has a lot of cool features but this one really doesn’t float for me.
Nice read. In the “Introducing the New Record type” the two examples are the same. Before and after the becomes. I’m assuming the first one was not supposed to use init.
class
was changed to record
in the type definition
Shame that records only work with reference types so far.
I agree. Structs are supposed to be immutable, so the new syntax would be great for them.
What I like about records is that the compiler synthesizes many boilerplate methods you end up creating just to compare classes.
Methods for value-based equality comparisons
Override for GetHashCode()
Copy and Clone members
PrintMembers and ToString()
Your examples of comparing car records makes use of it but you didn’t explicitly call it out as far as I could tell. Thought I’d drop a line here.
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9#record-types
T’was a great article. Thanks!
Record performance has not been great from the little I have researched: https://gmanvel.medium.com/interesting-performance-implications-of-c-9-records-equality-check-f0d0a3612919
The C# records link also points to the nullable reference types page.
One line type declarations with immutability is a dream come true for web request response pattern in a asp.net core API.
Great read, thanks!
Because deep down inside c# wants to be f#. j/k?
Yo tengo un helado sobre el pie!