C# Language Highlights: Nullable Reference Types

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] so matt i am sick and tired of seeing those null reference exceptions all throughout my code but i heard that c sharp has a new feature that can help get rid of them or make it a little easier so can you tell me more about those hey leslie yeah i'd love to and it's called nullable reference types and you might be thinking right off the bat it's like well of course you can have nullable reference types in c sharp but what this does and that helps really helps you make your intent super duper clear when you're writing classes so how about we jump into some code so i can make my intent clear about what i'm talking about yeah all right so what i have here is i created this survey question class and when you have a survey question you're gonna of course want some question text behind it i mean why would you ask a question without having the actual question a little awkward yeah a little awkward for sure and so what we can do here is make sure our developers who are using the class make sure they know that they should really be required to enter these question texts to it cool so how do we set that up yeah for sure and what i'm gonna need to do is go into the cs proj file and in here i can do nullable enable nullable close that out so what that's saying is you know what we're going to enable normal reference types here across the entire project it's going to go across the entire project and so you'll notice one thing that happens here is the squiggly line now appears on question text everyone's favorite and it's saying to us you know what you made your intention super specific and that question text now it needs a value yeah so it's looking like that default constructor is not going to fly here and we need to be more specific about exactly yep in order to get that squeaky squiggly tech's out of there we got to put a new constructor in and so all we're passing in is the question text and everything's cool we're all awesome that's really simple so let's make it a little more challenging so i've seen in surveys uh sometimes having survey hint options and this is completely optional but let's say we wanted to implement something like that while also still addressing the potential null reference issues how do we do it yeah and so yeah that that's the place where these nullable reference types shine and so how we would handle that is this way you'll notice right here after the string data type when i declared the property there's a question mark and so what i'm saying as the developer of the survey question class is that you know what question text i'm being super strong here you need to put that in but for question hint by having that question mark after it i'm just saying you know what i don't need it you know you can it's up to you whether you want to put it in or not it's so easy yeah it's pretty awesome so let's go into on the program class here and actually write a little program to use it well the first thing i'm going to do is create a question putting it into a list and just using the basic um constructor you'll notice i'm not using at all the question hint cool looks good so far so far so good and so but then for the second question i am going to use the question hint so first one doesn't second one does and so this is all building up to the fact that i'm going to loop through the questions and display the question and if there's a hint i want to display that as well the question is if though if there's exactly and you can see right here that's what we have to do is going down is i'm checking you know what if question hint is equal to null and it's even telling me hey you know what question hit maybe null here um i'm going to check for it put in an empty string if it is make sense put in the question put in the question and if it's not and then question text i don't need to check it because we already defined it we it's already defined it's the constructor yep yep so yeah that's what that's what noble reference types are yeah super cool super easy and it tells my developers or even tells me when i'm using the classes exactly what i meant to do that is really nifty and i love how specific the warnings are even as much as i'm not a fan of the squigglies kit i feel like that could save me a lot of exception handling issues so if you're excited to try out no reference types be sure to check out the docs below [Music] [Music] you
Info
Channel: dotNET
Views: 12,556
Rating: 4.9686275 out of 5
Keywords: .NET, C#, csharp, programming, languages, programming languages, dotnet, highlights, visual studio, null, nullable, types
Id: bXsOOpWavp4
Channel Id: undefined
Length: 5min 24sec (324 seconds)
Published: Fri Jun 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.