C# 9.0: Record types

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to this video my name is phillip eckberg and today we'll be talking about record types in c-sharp nine with almost every new version of c-sharp people have expected record types to be a part of the set of new language features however due to how complex it can be to implement and with the high standards for the developer experience the feature has been cut out of all the recent language updates i've hoped for record types to be a part of c-sharp ever since the first stable version using the new compiler was released now almost six years after that we're finally getting this added to the amazing language features that c-sharp provides but you might ask yourself what is a record type and how will it change the way that i and you write software in short it's a compact and easy way to define a reference type that automatically gets value-based equality as well as being immutable by default so let's break that down and first talk about the compact way of defining this record if you want to run this locally on your machine in visual studio for instance remember that you must install and use dotnet 5. now instead of using the class keyword we'll say that we are creating a record this record that we are creating will represent a person that has a name as well as an age this very much looks like a method what this syntax does is that it generates a whole bunch of useful code behind the scenes a lot of these new language features are just compiler magic that generates all the boilerplate code for us if we inspect the decompiled code we can see that this generates a class hence we have a reference type if we scroll further down and look at the functionality that this provides we can see that the backing field for the name as well as the age properties are both read-only this means that this reference type is immutable in other words we cannot change any of the property values on this particular instance of this class to get a person with a different name and age you'd have to create a new instance of the record so why would you care about immutability because in a more concurrent and asynchronous programming world we can now trust that the objects that are passed between different threads don't change the other thing that we can see if we scroll down a little bit further is that it introduces an equality check let me simplify what happens here what we see here is a value-based equality check this means that if we compare two instances of a person with each other it will compare the name with the name and the age with the age of both these different people if you compare two normal classes with each other that don't override the behavior of the equality checks it would only compare the reference obviously this is a very powerful language feature that allows for less boilerplate code it's especially handy in larger applications where you have a lot of models or dtos which normally contains a lot of repetitive code using the record keyword will generate the following different things inside the class behind the scenes we will get value-based equality it will override git hash code we will get a method for copy and cloning the record we will get a method for printing the members and an override for two string it will also introduce a deconstruct method which allows you to deconstruct the record into a tuple thus making it easy to apply this in pattern matching records also support for the traditional approach when creating the type we can define properties in the constructor we can even introduce method or use inheritance consider the following scenario where we want to introduce a subtype for the record person we could introduce a new record which we call developer that will inherit from the person record this will automatically give it the same capabilities as the person record we also want to add an extra property to this developer which is the developer's salary to spice it up a little bit let's use a more traditional approach of creating the property on this type just like when we create a traditional class we can introduce a property we can also create the constructor which needs to take the required parameters of the base record and pass it on there is no way of creating an instance without passing all the required parameters just like a normal class when you have one primary constructor we also want to take this salary as a parameter to the developer record and set this inside the constructor this looks very much like a normal reference type but with the main difference of that it's now automatically generating all the methods and the functionality that we talked about earlier taking this approach shows that we could now add methods fields and other properties just like we do with a normal class this record type that represents a developer also have value-based equality out of the box just because it's a record if we try to compare the developer with the person even if they share the same values this would fail because they don't share the same type the value-based equality check will also make sure that you are comparing two types that are equal the question is do you think that's a good or a bad approach would you rather just compare all the values and ignore the type i'm sure that ignoring the type would introduce some really annoying bugs for a lot of developers now let's also see what two string generates because this is an interesting addition to the record type as we run this we can see that it prints a beautiful representation of the record type now finally you're probably wondering how you'd get a new instance with a different value since the record is immutable we cannot simply set the property or the fields to new values we must use something called the with expression the syntax is simple we can say that we want to take the instance of our record but with a slightly different value this here would yield in a new record instance with different values meaning that the original instance still has its unchanged values this my friends is record types in c-sharp nine if you want to learn more about this you can read a detailed explanation on the microsoft docs i'd love to hear if this is something that you'd use so please leave a comment if you think this is a nice addition to the c-sharp language is this a language feature that you're going to use and have been longing for or do you think c-sharp has enough features if you like this video please leave a like leave a comment or subscribe to the channel for more videos in the future you can also check out my courses on pluralsight thank you for watching this video my name is phil beckberg and today we've been talking about record types in c sharp 9. [Music] you
Info
Channel: Filip Ekberg
Views: 14,072
Rating: 4.9124088 out of 5
Keywords: C#, csharp, programming, Filip Ekberg, dotnet, .NET
Id: oSQDZ7GpKwg
Channel Id: undefined
Length: 9min 24sec (564 seconds)
Published: Wed Sep 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.