My favorite new features in C# 9 (.NET 5)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to dot net core center this week we had the dot net conference and in dot net conference the dot net 5 was officially released along with dot net 5 c 9 is also released so in today's video i'm going to talk about three features from c sharp 9 which in my opinion are going to be extremely handy and very useful going forward so before i start discussing the features one thing i have to mention is that to be able to use c-sharp 9 and dot net 5 you will have to have visual studio 16.8 or above available and as you might already know dot net 5 is the next version from dotnet 3.1 and this is a version of dotnet which integrates everything into its umbrella and the reason the dotnet 5 was chosen so that there is no confusion with dotnet version 4 dot star which is the available version and they wanted to get rid of the core because going forward this is the net version there is not going to be any legacy version dotnet core is the going forward version so hence they don't want it to keep the core so that's why it is dotnet 5 just to avert confusion though for asp.net it is going to be still named as asp.net core5 it's because there is already an asp.net mvc5 so here to avert confusion the core is retained which is a little bit confusing but that's okay and same thing with entity framework core entity framework five is going to be named as entity framework core five because there is already entity framework five and six so that's some of the background about dot net five now let's start with the features so the first feature that i'm going to talk about is going to be init only setter let me first create a sp.net core web application in dot net file so the visual studio workflow remains pretty much same and here i'm going to say dot net 5 dot demo i'm going to create and in the next page i'm going to select sp.net core web api and see here i'm selecting asp.net core5 if i go back for a second and if i select a console app instead then what is going to happen is it is just going to go there into the project the new project will be created and in the new project i'll have to select dotnet 5 as the runtime so that's the fundamental difference now i'm going to go ahead with asp.net core now in this case i can use dotnet console as well but with asp.net core one of the feature that i want to discuss will become little bit more interesting hence i am choosing to use asp.net core so i'm going to select asp.net 5 and see here the new version of visual studio comes up with an enable open api support which basically creates swagger by default into an asp.net core application now once the project is created this is the default application with you know weather controller and weather forecast as a model so let's just start with this model itself instead of creating new models let's just start with this model so let's first discuss what is init only setter now you see this the weather forecast type now in this type we have four properties the date the temperature centigrade and summary these three have both greater and sadder temperature fahrenheit is just together and it is just returning a calculation out of the temperature c now with getter and setter the problem here is the object which will be created out of weather forecast is mutable meaning after the object is created we can go ahead and keep changing the properties so to demonstrate that if i go here we have this now let me just change this implementation a little bit so that i can show the features so if we do that and here i just do return weather now if we do that i can come here and say whether dot summary is equal to some random summary and this is totally fine i don't have any error there is no compile time error i can just go ahead and run this application and it will just work fine it's going to just update the property summary which was set earlier to this random summary and it will not even complain so if i go to weather forecast and i can i try it out execute i just see some random summary showing up in the response now if we want to make this weather forecast immutable then one way of doing it we create a constructor here and then we expect all the values the date the temperature c and summary to be passed in the constructor and we get rid of the setter so that's one way of doing it so we can do date and then temperature and string summary so we can do that if we do that here we can say date is equal to date temperature c is equal to 10 and then finally summary is equal to the incoming summary and i can just get rid of the setters here and if i do that now if i go here i first of all i cannot use this syntax so it has to be the constructor syntax and here i cannot do weather dot summary it says that the property is read only so i cannot set it so this will this will do but as you can see there is so much of boilerplate code that is needed to support it now what we can do instead let me just undo the code and keep the previous code and go back here and now in this case what we can do is if we do that of course nothing will work because because now this property there is there are no setters so they are all read-only properties so what we can do here this is where the init only setter comes in so we do init and then for all the properties instead of set we declare in it now what the init does is it allows you to set the property during the first time the object is created so when you are creating the weather forecast if you have inline declaration of the properties it is going to allow it but after that it is not going to allow so see here it says init only property or indexer the weather forecast or summary can only be assigned in an object initializer that's all you can do now if i just get rid of summary here i'll still get the same error because it is still not part of the object initializer so the only way to attach a property is through object initializer and this is a big big change it is not so much of a new feature but it is an extremely convenient feature which can get rid of lot of boilerplate code declaring constructor and setting everything up in constructor so this in my opinion is a very good and handy feature so let me run this now so if i run this now i still get my immutability at the same time i don't have to have a lot of boilerplate code so this looks good so this is the first feature that i wanted to talk now let's get into the next feature so the next feature which i want to talk is record type and this is probably i would say my favorite feature so far and you will see why let's go back to the same example here and in this case here we can go back to the weather forecast and now you see that we have date temperature c temperature summary it is still a lot of code instead of now using weather forecast as a class what we can do is we can define weather forecast as a record type but the real advantage comes is when we just do this now here is the biggest advantage right now we got rid of all the boilerplate code literally here if the temperature f property did not exist yet for example then that's all we needed that's all but let me keep it and now if we go back the only change we have to do here is we have to have object initialization so this would be the syntax and that's about it now here after we define a record type if we want to do we are going to start getting an error because record type as you can see by default record type setters are init only internally this is a syntactic sugar which allows us to create through this syntax but in terms of implementation they are init only properties so they can be set only during the object initialization that's about it so this gives us the same immutability that we want but with so much less codes there is no boilerplate code at all here and this is beautiful so now if i run the program my response is going to remain exactly the same and i go try it out and execute and i get the exact same response as before so this is really cool there is another important feature which comes with record type which is a width expression so what is a width expression with expression essentially let's say we want to create another weather so let's say we do var new weather is equal to and here we can say weather width and we can change one of this property or all of this property changing all of this property with wheat i don't know if it is like practically makes sense but yeah this is something you can do so let's say we just change the temperature and keep the temperature as constant give temperature is equal to 55 so now we create a new weather and i can just do console dot right and we can do new weather dot summary let's just print out the summary so with the weak keyword now we essentially created another object which is taking everything from weather and changing one or two properties so this is also here where it is basically doing an initial only and you can see that the temperature c is showing in it so it is getting everything from the base object which is weather and then just initializing the temperature c here now if i run this application and go and execute this i can see the new weather summary is showing up for all of them now the summary is not going to change summary is just coming from whatever the base class was so we can add the weather also here so we can say new weather dot temperature c and this is what we changed now if we run what we are going to see is we are going to see that the summary is going to remain what it is with the main weather object and here we can see that this is the summary is cool freezing mild balmy and sweltering and if we go to the printout we see the same summary but the temperature is all 55 whereas temperatures are different for this weather object so this is a way of creating a new object out of an existing immutable object with a different property so the key here is we are able to achieve in terms of creating a new instance from an existing object with only properties that we want to change and keep the immutability so this is something i think it's extremely powerful especially the cases where you get an object and then you want to update couple of properties but want to retain everything else as is in that case this gives a very easy way of doing it an extremely less amount of code so this is the second feature i wanted to discuss and last but not the least what i wanted to discuss is top level statement so what do i mean by stop level statement so top level statement is if i go to program this is the top level statement the program class and then public static void main now for all practical purposes this doesn't serve any purpose it is just there a boilerplate code not useful at all so what we can do is in dot net 5 we can get rid of everything and we can just do system dot console dot writeline hello world and why i am writing hello world because in every language when you want to teach someone programming you start with the hello world application now in dotnet 5 it becomes super simple it's just one line of code and you run it and we're going to see hello world in the console this is fantastic and now the next question is yes of course this is not useful this is not what we are going to do in real life programming so i'm just going to ctrl z get back to the previous state now what we can do is the top level method we can access functions and other classes so i'm just going to get rid of these and i'm going to add the namespace for startup that's it now i'm going to run this application and it is going to behave exactly the same way as it did before so it's going to show up we're going to go to weather forecast try it out execute and everything works as expected and here i'm seeing the console.write that i added to the other class everything is exactly same and we don't have the boilerplate code of the program class and static void main and all these things these are not even needed anymore so question is when this is really helpful because anywhere the main is created out of box by the visual studio well if someone is not using visual studio then this is definitely helpful and especially if in my opinion this is extremely helpful when you are writing server-less functions so if you are using azure functions or lambda this kind of code is going to be very helpful that's why i personally see that a lot of the lambda functions end up being python or node because of the simplicity of writing everything in one function now this gives us exactly same capability in dotnet so that's why i really like this feature so that's all i wanted to cover today if you like this video please give me a thumbs up and if you are new to my channel and if you think you are getting value out of my channel please subscribe to my channel and thanks so much for watching this video
Info
Channel: DotNet Core Central
Views: 20,680
Rating: 4.7976656 out of 5
Keywords: c# 9, .net 5, c# 9 features, .net 5 features, c# 9.0, whats new in .net 5, whats new in c# 9.0, whats new in c# 9, init only setters, record types, c# 9 init only setters, record types c# 9, top-level statements, c# 9 top-level statements
Id: 1uERXdj5i7k
Channel Id: undefined
Length: 16min 51sec (1011 seconds)
Published: Sun Nov 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.