C# Null-Conditional Operator (? Operator) Explained!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in one of my videos i was using a line similar to this in my sample code and there was a question about the question mark so i'm going to make this video about what the question mark does and that's the question um this video is going to be about null conditional operators in c-sharp let's dig in and learn about what it does [Music] so let's talk a little about the null conditional operator so basically whenever you have the question mark dot and you can also do question mark and then the you know the hooked hooks if that makes sense um to you know make an index for the collection though this is the docs page for all of it this is actually the docs page for member access operation operators and expressions so this lists all the things it even explains like you know whatever you want to do member x's expression which is just you know whenever you press the dot to access a member on an object but the thing that we're especially interested in is here the no conditional operator so this is available from c sharp six which has been out for a while at the time of recording we're at c sharp nine i think c sharp ten they're making plans for that so it's been in here for a while and they explain it like if a evaluates to null then the result of a question mark dot x or a question mark indexer x is also null if it doesn't index to null then it will just return the object so you know and they have some sample here but of course i wouldn't be making this video if i wasn't going to show you how to actually do this so let's switch over to a new tab which is try dot net so try dot dot dot net that seems like a clear url right but this is really really cool um because you can see here there's already like a little text box that holds some g-sharp code and i think this is running on blazer and so there's some sample code in here this is just if you create a file new console application this is basically what you get so this is the entry point for any application in net and c-sharp so here you have the the main method this gets triggered whenever the application starts and here it does for each for 20 something it will do print the fibonacci sequence right so whenever i run this you will see the output here at the bottom and this will be the fibonacci sequence so that is cool but that's not what we need but this is what try.net does like i said i think it runs on blazers that's cool it runs probably on the client and you can just execute all kinds of c sharp things so i really like to use this tool to you know try some things out in in c sharp but let's remove this little fibonacci method right here so we're just you know this does nothing this just executes main but it does nothing so but let's create a little public class person and there we go person you can see the intellisense is working too which is cool and i'm going to give this a public string name of course which is you know a property get set there we go or a public person let's let's nest it a little bit person which is going to be the partner get set so this is just you know an object that you would typically implement for something and a public string array um hobbies let's make hobbies get set there we go so let's take this step by step right um so let's go use the name first so i go in here to the main and i say var let me let's actually do this explicitly person person is let's make it null to make it interesting right away so here we go person and we're going to say console.writeline is person.name there we go so whenever we run this then take take two seconds what's going to happen it's going to crash right because this is null so whenever you call person dot something then it will crash and it will give us a no reference exception because you know we're trying to reference something on an object that doesn't exist which has null and that is kind of the problem that we have here right so typically what you would then go out and do is check if person not is not is null typing is hard null then we're going to do this right so whenever it's not null then we're going to to actually do this and you run it and then nothing happens because it's null so it skips over that and we do that but whenever we do this that new person and we can do it like this um name is gerald there we go and whenever we run it now gerald will show up so that's fine um and also without the check it shows up right because but now you have added one two three lines of extra code um that you know it's important but it's not necessarily something you want because you know you have to say to do the same thing for partner maybe for hobbies so that's not great right so what we can do with this conditional operators is it's the question mark and we can check if something is null so we can just do it in line basically so if we remove this if statement right here then we can now say person question mark so i always like to think of it like hey is person null we're asking the system if person is null so please give me person dot name and whenever we do this it will work so nothing changed it works but whenever i change this back to be null it will still work it won't give us any output of course but it will still work so what this does is it says like hey if person is null then um i'm just going to skip over that and i'm going to um actually it it short circuits so it will it will just stop evaluating here but it will not break right it will just make this null and apparently console.writeline checks for null and then it will just output nothing so to make this even more clear i guess we could do if string dot is null or white space so this is a method that checks a string if it's null or it just contains white spaces and if i say here person.name and i'm then going to console.writeline and say a person has no name which is you know an obscure game of throne reference and whenever i run this it's going to crash that's what we expected right because we don't have that null check in here so what it's going to do now it's going to see like hey i want to get this person dot name and then it says whoa but i don't have a person so i'm going to crash so again let's just fix this by adding a question mark in here and now we're going to say run and it says a person has no name because it will now come up with null so we can make this even more clear by saying console dot right line and let's check if person dot name is no right and that should evaluate to true there we go so this comes up with true so basically each step you can also nest them so if we say person dot partner oh and then we have to chain of course the question marks too so now it's going to see hey is person null if this is null then it's going to stop evaluating here and then the rest will be null too but if person is an actual thing but partner is null then it will stop here but if partner is also not null it will actually evaluate the name so let's just remove well let's keep this in here let's keep these things the same dot partner so it basically just you know goes along with this it still is nothing but you can chain them like this so that is cool right and if i remove this one here then it will crash again well it should crash again oh that's interesting so you don't actually have to do this that's cool oh interesting well now i've learned something um so but i i would opt for just being sure um adding these here and now whenever we save our openness for his new person and i can say name is uh gerald again so that works but we need so we don't we won't do a partner so that's run and now my my name is coming back right because it still skips over here and it gives me the name but it still um you know just does evaluate the the partner and stuff like this and if we say partner is new uh person again um then give that a name is also gerald because you know i'm just my own partner and then whenever i run this it does not contain a definition for partner that's right part nur there we go and if you run it again you can see it skips over this because you know now it's not null or white space it actually has something so let's copy and paste this one and we can say person dot partner dot name let's add the question mark and now we have also gerald see so that works now for the last bit for hobbies you can do kind of the same thing so let's add some hobbies in here so let's do a little comma and say hobbies there we go howie's is new there we go and let's make this uh being a youtuber there we go and uh gardening because i love some gardening and of course um programming there we go so now i got a couple of hobbies and whenever we do a console.writeline i can do string.join let's say put a comma between it and make it person dot hobbies there we go so whenever i run this we have a syntax error because i forgot the comma here run and now i'm getting all these so that is the hobbies but let's change this actually to be just one hobby um best oops best hobby there we go plus person dot hobbies so let's take the first one run and i'm getting the first one right so best hobby is being a youtuber yes subscribe to the channel of this youtuber if you haven't done so already so you know this works but whenever i change this to be also no there we go no then it's going to be null right because that is kind of like um it's it probably evaluates to string dot empty because of this i'm not actually sure but let's you know remove this whole thing and then it's going to say again like system no reference right so what you can do then is whenever you're not sure when this is going to happen you can also add a question mark in between here and you can say hobbies maybe 0 or it could be 1 or it can be 100 it would just check first if hobbies is null yes or no and then it will evaluate the index yes or no so but also you can change this again right so if this is actually a complex thing then you can also go back here it's not going to be the best syntax but you can also do the question mark here and do a dot with after that right so you can chain it again and whenever something is null or not null it will just follow through with all the rest i hope this was a little bit clear this is how to work with no conditional operators in c sharp i hope you now understand how the null conditional operators work in c c-sharp and net so go out and simplify your code because this should be able to save you you know like a couple of if statements check if person is null then check if person.partner is null and then go from there you can remove all of those and just put the question marks in there which is of course really really cool of course beware as always because you know these are nice little syntax syntactic sugar because it makes the syntax a little bit different a little bit nicer but you know be careful that your code still is readable because um it might not be so clear to other users who are reading it after you if you've liked this video please click that like button on the down here um and if you've subscribed not subscribed to my channel yet please subscribe to my channel if you've liked this video and maybe there's there's lots more so go check it out before you know i can convince you to actually subscribe it but please do and for the rest i'll be seeing you for my next video keep coding
Info
Channel: Gerald Versluis
Views: 1,351
Rating: 4.9565215 out of 5
Keywords: c# tutorial, null conditional, null conditional operator, how to use null conditional operator, null conditional operator c#, learn c#, c# tutorial for beginners, learn c# .net, learn c# for beginners, c# 6, .net, .net tutorial for beginners, .net core tutorial, C#, c# 6.0 interview questions, c# .net tutorial for beginners, null condition in c#, c# operator for null value error, null conditional operators, c# operator for null exception, .net core, conditional operator
Id: eOpmjGF4qxo
Channel Id: undefined
Length: 13min 26sec (806 seconds)
Published: Sun May 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.