How null checks have changed in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody i'm nick in this video i'm going to take a look at how null checking in c-sharp has changed over the years because it's an interesting topic that you know we started with something we thought was the norm and it's no longer than normal something else is normal and i want to see what changed and how it changed i just find it fascinating so if you think it's interesting stick around with me we won't really touch into the null reference types feature of c sharp 8 if that's your concern of if that's what you're going to get out of this video this is more about good all regular null checking so if you find that interesting just stick around and let's take a look at the code so what i have here is four projects and the first one starts with c-sharp six which is the last version before bigger changes started happening and what i have here let me quickly show you is i have a simple class that i call my special class which only accepts a name for an argument and it converts it into a property so that's it and then i have this program.cs to start and basically this has some chance to initialize either this class or set it to null and why do i start from version six well in version six live is pretty simple all you need to do to check whether that is null or not is value is null and then do a console. i don't know right line value was null and if value is not null which is how you're doing the opposite then you say value was and then string interpolate the version and by the way these projects are all limited to the version i'm talking about so if it was a feature of a different language it would throw an error so that's simple enough and i can just run this real quick and show you that yeah the the value was i should actually get the the name so very straightforward there's nothing else really you can do here to spice this up the other two things that we have is that you can do for example that if value dot name equals null and you can use the comma which is the null propagation i think operator which means that it will check whether value is null and not so whether name is not will propagate the null and this is just another feature of this version and you also have the nulco lacing operator which is like a var test equals let me move this up equals value and then you can say new neck and what this will do is check whether the value is null in line and if it is null give you a new version to assign but fundamentally these are the two ways you can do another check in in cs6 and before that and it worked great for us but then c sub 7 came into the game and it spiced things up a bit with pattern matching so now nullcheck like i said used to be this console a write line value was null but you could also do this value is null and this is a pattern matching feature that was introduced in c sharp seven and it turned out that this and for the years to come for the next versions this was a safer version to do a null check and the better version if what you wanted to do is to make sure that the reference of this value is not but this is null basically and why is that well i'll show you why i'm going to delete those and i'm going to to debug obviously the opposite of this at this point is is this so you have to wrap this into brackets and say is not is null so it is not null uh there's no other way you can invert that but there is also another way you can do a is not null check you could say if ah come on my fingers if value is object and this is basically a value is not null pattern matching based check and obviously this is not readable at all but it will do the same thing it will ensure that the value you're checking is not null now the reason why i'm saying that is null is safer than equals is that i could very much go into this and override the operators so the equals operator and the is not operator can be overridden the ease operator cannot it will always be unaffected and will always check whether the actual reference is null or not and i can prove it by sticking a breakpoint here and debugging now i have configured this class as you can see to always say that equals something is true so even if this is not null which in this scenario it is not there is null check will fall through and as you can see i went inside uh let me prove that again with a console right line because it might not be clear this was null and if i run this again and it's it's such a an interesting one so as you can see now this was actually null so if i rerun it hopefully yeah so this is not null the value is test as you can see and if i step in this it goes into as if the value was null but if all i'm doing is in nullcheck this is wrong obviously you have to be insane to override the equals operator like this anyway but you could have some more complicated logic here which could cause this being a legitimate bug so his value as you can see will step over because it wasn't knowledge just skipped if it was it would go in here and again is not null will fall in here because the value is not null and then is object which is the is not null will also go inside so these are the the new ways and i i would argue that this is not really readable because you can miss the exclamation mark and then this isn't really readable as well because like what are you checking you're checking whether it's object it's not clear to me that you're doing a null check here so the positive version makes sense but the negative version doesn't really make sense but it should be clear that the difference is one is is dangerous ish because you can override the operator while the other one is not because you only check in the reference the other thing is that if i use the il viewer that writer supports you can see here let me just make this a bit bigger you can see that is null on the checking operator we use the sec op code now you don't really need exactly to know what it is all you need to know is that this is comparing two values but the equals since we're overriding the operator we'll call the op equality method the operator for equality method and this is a bit more taxing performance wise because it will need to actually call a method behind the scenes while is will just do a straight check on the stack for the two values so it's interesting that it's not just the dangerous nature of the equality operator it's also that there's a performance improvement as well and i just thought that is an interesting point to touch upon of course if you weren't overriding the operator and actually let me just show that because i think there is value in showing this if i delete this and i rebuild it so the op code gets updated you can see that both is null and equals null will use the second code so the call of code will be used only when you actually overriding the equality operator but with that out of the way i think it's clear to understand what is the difference between the two now this version also had a few other ways to do null checks for example you could do underscore equals value null collation operator and let's say throw new argument null exception name of value and this will basically throw whenever a value is null it it's essentially a shorthand version of if value is null then throw new argument exception it's very much the same thing but it's doing that by disposing the property that you assigned to or the value you assigned to so it's definitely less readable but more compact uh i usually it's an hours like this i usually go the long way because i think it's more readable to use it but this would also work fine uh if you all you want to do is this type of check and with that out of the way actually let me move this at the bottom because you might want to use this code for testing locally and with that out of the way you can go to version 8 and version 8 is a bit more spicy it basically carries over all the seven features but it does add a couple of things because microsoft said okay value is object is not readable if you want to do it is not null check so how can we make this better and what they did is they said you want type object you will put these curly braces this is not a joke i'm serious they actually did that this is the same as saying basically object and it is a is not null track uh right line console.writeline actually what am i doing console.9 um value was not null value is and i'm gonna put the name name so this will only print if the value is not null so we have a 50 chance of this actually not working okay so value was not null value is test and the only way you can do this is if this actually had a value and if i run it again it probably won't print anything yeah because now it was not so this is a way in c sub 8 to say value is not null and this is the like equivalent safe way of saying if value is null so it's the opposite essentially interesting but not readable at all so i wouldn't recommend using it because it's very hard to actually understand unless everybody's aligned to know what this is doing now another feature that was added in c sharp 8 was the null co-leasing assignment operator which is basically the non-correlation with an equals and what this does is it checks that a if value is null then assign it to whatever so this is now nick and if i debug through this you will see that is this null no this is actually has a value so good so value equals test for this version and if i skip over this line nothing happened it still test but if i run this again and hopefully this will be not this time i was unlucky i have to run it again i'm gonna cut this later now it is null so now that it is null i am able to use the null collection assignment to set the value not really a direct null check but it's a null check and an assignment if you wanna make your code more compact but the big thing really was this um you could do a not null check in the safe pattern matching way of checking for nulls without having to use his object which didn't really fix any problems and c sub devs actually heard us about that and in c sharp nine the de facto like quote unquote proper way of doing null checks if you want to check that the reference is null and not want to involve any operators in the mix is to say if value is null and if value is not null these are the two ways that you want to be doing null checks now i know this is a departure from what we've been used to but in terms of what the il is being translated to and in terms of the safety and readability i think this is actually a huge improvement and i'm a big advocate for using this feature so that's it for this video a smaller video just talking about how null has changed and how not how module is looking like modern world checking i think that the c-sharp team has done a great job moving towards a safer null and i'm a big advocate for it as well so good job microsoft that's all i have for you for today thank you very much for watching special thanks to my patreons for making these videos possible if you want to support me as well you're going to find the link in the description down below leave a like if you like this video subscribe for more content like this ring the bell as well and i'll see you the next video keep coding
Info
Channel: Nick Chapsas
Views: 86,191
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, asp.net, .netcore, dot net, core, C#, how to code, tutorial, asp.net core, js, csharp, rest api, lesson, dev, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, asp.net core 3, .net core for beginners, .net 5, asp.net core 5, How null checks have changed in C#, c# 9, c# 8, c# 7, c# 6, c# null checks, c# is null, c# is not null, how to do null checks in c#, dotnet, .net
Id: lRUfRlp5BXc
Channel Id: undefined
Length: 14min 21sec (861 seconds)
Published: Wed Jan 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.