No more null exceptions in C# with null-coalescing, ?? and ??= operators

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're getting into software development the first thing that you're going to probably run into are null so let's just be honest about it objects properties null reference exceptions this is a thing that can cause headache for developers like if you're using c sharp there's a bunch of cool things called null coalescing coalescing coalesce choleskus whatever it's pronounced basically it's a way of dealing with nulls so you don't have to do a bunch of random null checks all over your code now i've done a bunch of videos with myra over on the down at youtube i'm going to post those as well there's an entire language features highlight series but i posted an image right here on twitter and i got a lot of questions around what is this question mark question marks equals things and you know we covered it but i'm going to cover it again because there's a lot of different really cool things with dealing with nulls in c sharp that you need to know as you start to develop applications and just so you don't get those no reference exceptions so let's break it down with our null coalescing 101 overview today so tune in [Music] all right let's see how we can handle nulls and get rid of null reference exceptions with c sharp with some no cola saying let's jump into some code all right i'm here inside of my.net interactive notebook and the first thing that we'll notice here is that i created a class of person and it has a single property of name now these are both object types they're reference types so so here i have to create the class i have to create the object in memory so this is a person and the string is also an object and i have to create that too and this is important to understand how this is different than like a value type as like an integer and a boolean is because if i don't initialize these these are null which means they haven't been initialized at all whereas an integer or a boolean or double has a default value so integer would be like zero or false for a boolean now you can make value types nullable but i'm not going to get into that today but what happens when things are null so point in case person i have my person here when i create this person here i haven't created it this is sort of initializing that i'm declaring a person of person type but i haven't said equals new right i haven't said you know equals a new thing at all yet so here when i say person.name person is null which means i get an exception now i don't even have to be outputting anything to line here but we can see i get that object reference exception now if i did come in and say new person over here and ran this code again now that exception goes away because even though person is null name is also or not null name is now null and that's okay i'm here now the reason that this is important and i wouldn't want to do this because if i didn't initialize it i might have to do like you know if person equals null or does not equal null you know then then actually go ahead and get it and then return this stuff so i could say okay then i'm going to do this i'm going to index in here and then i can say okay now i can run this code and if i have a bunch of different properties and objects i'm going to have to write all this code all over the place that's going to be kind of annoying because now my application doesn't crash but i'm going to put null checks everywhere so we don't want to do that all right so we want to go ahead and make this super simple so i don't even have to initialize it or see who's initializing it and carry on so the first thing is well how do we check and guard ourselves against null when person is null because that might be inside of a you know inside of a method that takes a person in and i have to do all those null checks well what we would do here is add a little thing called a question mark this is our first guard against nulls in our code now this thing is beautiful i love it question mark dot if i don't have question mark and i just do a normal dot it literally will no matter what say on person call this method or property on it right so if i had something that was like print name or last name or whatever it would call those but when i add that question mark it's now going to like check the object it's going to say hey is person null and if it's null stop just return null just return no we're good we're out of here we're out of here right but if it's not null continue on all right so check this out now i run it and i no longer get that crash in my application so now if i wanted to i could go in console right line we're good to go now the problem here of course is that if i come in and i try to do name dot length off this name is now null right so person is is is not null is null um so it stops but also name is null and we get the same issue here which is if i say new person and i run this i get the same problem here because person is no longer null so it does call name but now in this case name is null so it outputs nothing and then throws an exception here now of course i could come in and i could say name equals james and then now everything's working fine right so james and the length is five and we're all super happy and good to go but that's not always the case all right sometimes we have nulls in our code right and we get these exceptions that are throwing out so what if we wanted to change this and just say length here and instead of outputting the length there well i'll put the length here again this can throw you know it's going to have no issues because name is null and we're going to output null but if i come in and say length right and i throw this now we get a null reference exception again but don't worry our friend our question mark friend is here to save the day and look at that person is not null name is null so stop return null and output that's a line the same thing is true here if person is null if person is null or name is null don't call length at all and continue on so this is fine but but obviously we want to like return something here right so this would be bad because we would be returning null if the length or the person or name is no we don't know what it is so you can check for null and return a default if anything along the way is null so if either person is null or name is null or the next thing is null in this case it's a length so it can't be no it's an integer we want to return something so in this case we might want to return negative one so what i'm going to use is no coalescing and use question mark question mark and what this says is anything before here null and if so just don't return null return anything on the right hand side of question mark question mark so now we get negative one now here i could also then say name equals james right let me go and put the double quotes in here there we go and what we'll see is well then the length is 5 because it's now james if i come in here and person is null boom negative 1. it said person is null negative 1. if name is null negative 1. if i don't do this right it's going to just return null so that's the difference is you decide what you want it to do do you want it to return null in this case because it hasn't done anything or do you want it to return a value and most times here the issue will be like you want this to be an integer right and you want this to be here and the problem is that because of the nullability we're putting into it behind the scenes you can see that this is turning into a nullable integer so we would almost have to surely return negative one so this will always give us an integer back automatically when we use var it's sort of handling it and hiding it behind the scenes all right that's sort of a magical thing that's happening there so so far what we've seen is we can use a question mark dot to go a little bit further in to our object into our class so if we want to and we have a property or we have a method or multiple properties deep we can go down that chain and check null that's going to get rid of all those null checks all over the place then additionally if we want to return a no a different value a default value if that thing is null then what we'll be able to do is use question mark question mark and then any time that something is null on the left hand side it will return the right hand side for us automatically that is really really powerful but in my tweet i use that question mark question mark equals what the heck does that mean that's a great question so let's go ahead and check this out so let's say you're doing some mvvm development and you have a view model so i have a bunch of nvvm videos i'll put a link up here but also this is the code behind and here i might have a person view model and i have a person and then i have a public person that's the thing i'm going to access so i might modify you know the private you know person here and then you know change some properties on that name so for example we have vm you know person you know person view model and then i might write the first name and in here we're going to see that this is an exception because person over here is null so we could solve that and let's say this is a get only so i'll just do an assignment here and i'll say return person and if person you know um is null then let's return a new person and i'll say name equals james all right and we'll just run that so now we get james now of course if i did something like public person view model and just did a default constructor and i said person equals a new person over here what we would see is that this will just return nothing so in this case since person already exists it's going to simply return that person that's been created here but let's say i didn't have that right then what we're going to note is that it's going to return this over and over and over again now here is the problem the problem is that this is not a signing person all right so check this out let's say i did public void is person null all right and i said if person equals null then i could say console dot write line and i'll say it is null all right check this out so now when i run it over here and i now call vm dot is person null run this code again guess what it's totally null you're like what what what is happening right what is going on well this person is not a lower case person which means every single time i access this person whether i call you know this person or have a last name first name it's going to go in and create a brand new object over and over and over again and that's no good we don't want that to happen so what we can do with null call listing is we can do no coalescing assignments and guess what it just adds on to question mark question marks with an equals and you're going to see that squiggle go away so check this out now when i run this code james over here right and here i'll just say else right and i'll say counsel dot right line it is totally not null boom let me just run this again it's totally not null so i literally just assigned that person object to a new person so it won't call it over and over and over again and that's super duper nifty and you can call that at any time so for example over here i could say person question mark question mark equals and then i could say new person right i could say new person here and this works right inside of here you can say person equals equals person blah blah blah blah and this would this would return us so to say you know we're good to go basically at any time we can run that logic inside of our code which is actually really really cool you can run that at any time now i want to show you that there's a bunch of great uh you know documentation here so for example i love this where they're talking about like here here's a bunch of numbers you initialize it to a new list you add five to it there's great documentation of how you can kind of go further here with the null checking so you can go further and further and further like all sorts of really cool examples of how this can work together you can use that null condition operator that question mark dot with question mark inside of an uh um index for example here this is really really neat so i'll put a link to this in the documentation so you can see what versions of c sharp c sharp it requires um and what allows you to do something like this check this out this is what i do inside my code so for example i made date time uh nullable like i would call this database and i get a bunch of rides and this these things are bound to the ui but they're bound to the ui at different points and times so i don't want to always call date time to local time and from ticks and from this and like all these different things so to store this value so it doesn't create it over and over again i use that null coalescing inside of here look at this it's beautiful i say display date equals this and it equals this so it only calls it once in your code and you're totally good to go and you can combine all these things and do all sorts of good stuff it's pretty spectacular to be honest with you i love it so you can do all these different things i also recommend checking out the c sharp language highlights there's all sorts of great things to learn c sharp features in fact like i said there's the um a null coalescing operator me and myra did one on it there's no closing assignment i literally already did these videos but i decided to go even a little bit further and explain them in in real real depth so anyways i hope that you enjoyed this longer form video of going real deep on the different null capability checking of c sharp and if you have any questions at all put them down in the show below the show notes in the comment section and of course don't forget to uh subscribe and hit that notification about super help the channel so you stay up to date when new videos come out but it helps the google youtube algorithm of goodness recommend this video to other people if you want to see more c-sharp stuff let me know put a comment below super appreciate it that's going to do it for this episode and this video um cool cheers [Music] you
Info
Channel: James Montemagno
Views: 8,777
Rating: undefined out of 5
Keywords: .net, .net core, C#, blazor, c sharp, c# 2021, c# maui, c# tutorial, c# vs python, dotnet, funny shows, james montemagno, montemagno, null, null coalescing, null reference exceptions, null value assignment, null values, value types
Id: 7KJmdE93HkI
Channel Id: undefined
Length: 15min 46sec (946 seconds)
Published: Fri Sep 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.