Working with Null in .NET 6 and C# 10

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
unless you're very new to c-sharp you've almost certainly worked with null in some ways it can seem very easy to understand however it's a lot more depth to working with null than it first seems in this video we're going to look at how to properly work with null in c sharp that includes nullable value types nullable reference types no conditional operators no no coalescing operators no coalescing assignment operators no annotation context nullable directives and the null forgiving operator and more that's a lot of stuff to help us work with null so let's see how those features you know how to use now if you don't know me my name is tim cord it's my goal to make learning c-sharp easier i do that by providing videos here on youtube multiple times per week plus i have a weekly podcast i also provide courses on c-sharp web development and much more at iamtimcorey.com the profits from those sales are what pays for the free content here on youtube so that everyone can have a great education in c sharp not just those who can afford it now in this video as with most of my videos i'm going to create some source code if you like a copy of my source code use the link in the description all right let's go to visual studio and we're going to create a new project all right keep it simple just do a console app because the point is not to focus in on which product type it is but on how null works in c sharp so let's call this our null demo and our null demo app next and we are going to use net six and this is important because some of the features where i use in this demo will be features that are pretty new to c sharp in fact uh c sharp nine i believe uh was the latest changes there might be a couple of tweaks from ten but i don't think so um but we'll definitely need the latest version so if you're on for example.net framework some of these won't apply in fact a lot of these won't apply and if you are an older version of net some of these might not apply as well but we'll talk about that as we go so once we get started one of the things that we're going to talk about is the defaults in the project i'm going to wait to do that so let's get rid of the sample right here and we'll just get a couple of blank lines so what is null well null is an absence of a value so there is a potential for some things to have a state that is not set or has no value has an absence of a value if we look at value types so for example an integer if i say int i the default value for an integer would be zero so that's the default value that i wouldn't have to assign now i do a more because we'll get into that but because of the fact that um it's by default not nullable but it still wants us to assign initial value to it but we could use this potentially because of the fact that it does have a zero for value now i've assigned it automatic or manually but the default when we don't have a value and integer the default value it puts in is zero all right however this is a value type value types since i believe c sharp 2 have had the opportunity to put a question mark at the end and this question mark it makes the value type nullable so this is saying this is a a nullable value type and what that means is that i can say no and that is the default value for integer if we don't assign a value is now null because of the question mark so this adds an extra potential state of null now if i were to take the question mark off that's going to give him an error because it says hey you cannot convert an int or null to int because it's a non-nullable value type so the question mark says well actually you can put a null in this variable now because it's a value type this does change what this is so there's a normal system 32 dot integer um i'm sorry system.in32 sorry but with a nullable int that's a different type now it's still an integer but it's a nullable integer and so it actually changes behind the scenes the type for us obviously we're declaring it as a little different but it is making a difference now that's important because we get into things called reference types now a reference type can be a string so string let's call it s and by default i can the default value for a string is no that's i'm just specifying here but that's the default all right but notice there's no question mark here okay that's because reference types are already nullable so another option for this let's add a class here so add a class we'll call it a person model and let's clean this up a little bit there we go make it public just because let's see in public and we're gonna do an int id and we're gonna do a string name is fine and let's do one more boolean and we'll say [Music] is is valid how about that so by default boolean is set to false okay an integer is set to zero and a string is set to null those are the default values so notice i had to mark the value type as nullable in order to put a null into it but with string i do not the same is true if i were to say person model which control dot add that using statement person model p equals new okay so this if i didn't say equals new i could say equals no and that fits just fine all right so i can put a null value into p and by default that's what the value would be of type p so this p right here the person model is a a reference type now if you're not familiar with the difference in reference type and value type is we go all the way down a rabbit hole and i keep it really simple and at the surface here um so value types here's the practical difference when it comes to using them if i were to say um let's just set i well let's let's do a demo down below we're not messing with what we have so far so if i were to say int j equals five and i always say int int k equals j all right and then we're set j equal to 10. now do you know what the value of k will be well it's gonna be five because j equals 5 put the value of 5 in the k but notice i set the value of j equal to 10 it didn't change the value of k because what happened was this 5 value was copied from j over to k all right so when we use value types and pass them around we pass a copy of the value so k is now 5 and j equals 10. that's a value type now if i were to let's do this um person model test one equals new how do i say person model test two equals test one all right and let's let's actually set the value of test one dot name equal to tim and now i will change the value of test one dot name equal to su what's the value of test2.name well let's just show you here console.writeline test1.name and we'll do console.writeline test2.name we're gonna show both of these names let's run this and get a value of sue for both of them but why is that because i copied test one after i changed test one's name to tim i put that value in a test two right well no i didn't put the value i put the reference so the this points to a certain address in memory this now has that same reference so think of it like a we built a house when we said new and so we built a house we got the house's address one two three sesame street and then we built another house and we said hey that address is actually the address of the first house which is one two three sesame street now they both have same address so that when we go in the front door and sit in the living room changing the name here that's what we're doing um we change it for both of those they both refer back to the same address the same location that's a reference type okay so now that you know that let's talk through these reference types here so we've seen that we have this p which is null and it can be null even though be marked as nullable all right because by default we can say it's not pointing to any specific reference it's not any address and memory it just is it's just it's waiting for that address in memory and that's where that equals new comes in so by default reference types like person model and string they refer back to a a null value by default so we've already had this this came out of the box with original c sharp but with c sharp 8 we started to say hey you know what that might be kind of problematic because we don't know if if this value is going to be null or not and let's create some kind of syntax around this that will help the compiler know and help warn us when we're using a value that might be null and that's where we have this concept of the nullable annotation context okay and that's a big word but what a big phrase what that is if you go to the project and look at right here line seven in net six and beyond projects they're new we have this by default nullable is enabled all right now if you're in a net five project or before then you will not have this turned on by default it will be disabled or non-existent if it's not existing it's disabled but for new projects only in net six going forward it will be on by default and what this does it allows us to know more context in the design time so that the editor can warn us about potential issues with our our variables where it says hey you know what you might be using something that could be null and we can hopefully catch more errors that way all right so with it enabled one of the things we can do is we can say you know what this string we expect it to be null at some point so you put the question mark after it so this is saying yes this is okay notice the null um had a squiggly underneath it wait for it there you go where it says converting null literal or possible null values and non-noble type that's a warning it's not actually a a an error but saying hey you're doing something that you probably aren't expecting to do you're putting a null value in a string even though it's that's allowed because you have not marked string as nullable then you're saying i don't expect it to be null okay and that's what this change right here did now let's just take this out for a minute let's just change it to disable we'll say that come back over here and when it catches up okay notice that null here and here no longer have a squiggly underneath it says hey you know that's that's fine you can put a null value in a string you can put a null value into person model because that's expected but now with this flag let's put it back to enable it's saying if you want to put a null in these these reference types you need to have a question mark saying yes this is expected to be nullable otherwise it should not be null ever all right and that's what you're you're telling your code that's all you're doing is telling your code that and the benefit here is that your editor will have a better time being able to warn you hey there's potential problems here there's potential issues so that's what our our nullable annotation context does for us is it gives us more potential warnings now if you are in an existing project which has hundreds of thousands of lines of code that could be overwhelming all of a sudden you have thousands of warnings and that's a lot and so there are other options for our our nullable uh annotation context so we have enable and disable and that's this is only for warnings by the way so if you change it to enable you get a thousand warnings you can still run your application no problem but you really want to clear those warnings out but in a big project that could be pretty intense so we also have the the option of making some tweaks so for instance we can say let's change this to annotations and what this allows us to do is it gives us no warnings so it says notice there's there's no issues found come back over here um those those issues are gone even if we were to change this um it's not gonna give us any issues but what i'm saying is that the three issues i have are because i've never used these variables that's why um but there's no issues here with our null and the reason why we've said hey allow us to annotate using those those question marks but don't give us the warnings yet let's just set things up okay so this is what you'd use when you're in the process of converting your application over and you say hey let's let us use those question marks which when you disable um then the question marks are kind of superfluous so if i say disable then these question marks right here they get a they get a warning themselves where it says uh the annotation for noble reference type should only be used in code within a nullable annotation context so it's saying hey those don't fit anymore or don't fit because we haven't gotten there yet this is c-sharp 7.3 and below okay where we don't have the the question mark for reference types just for value types so with changing this to annotations it allows us to put these question marks in place and warn us about them but at the same time if you don't have them everywhere it's not going to warn us yet about that either that allows us to slowly migrate our code base over to this point where we're able to to upgrade to the full enable where we can say yes we're using the nullable properly so we're decorating our code properly so that our code is going to give us the right warnings when we we potentially use something that could be no all right so that's annotations there's also one more state all right and that is warnings now what's warnings for we come back over here we'll notice that that question mark right there is lit up it says hey you can't use that question mark that's not that's not what's there for so you take the question mark out because we're not going to mark our our reference types as nullable but it is going to give us warnings when we go to use a potentially null value so for instance if i were to say console write line i dot to string well that would potentially be a problem because i is no and so if i were to run this it's supposed to give me an error it doesn't always it doesn't always know correctly what things are null what things aren't of course in this case it's my mistake because the nullable int actually does have a two string value whoops but if i were to say s which is a string and i say length that doesn't have so right here it gives me a warning dereference a possible possibly null reference so it's saying is this is the warning that says hey you know what you're using a value that might be null when you're using it you should probably do a null check first so this warnings right here doesn't let us put the question marks here doesn't let us make those changes yet but what it does let us do is it gives us the just the warnings in our code where the compiler thinks we might have a null value this allows us again to upgrade our project when we have an existing project where we don't want to see all the warnings we want to get a picture of how many times do we use variables that might be null we haven't checked for and this is going to say hey you know there's there's a problem here um you you're potentially using a string that's no now it's it's obvious the string is and all these is right there but check this out let's cut this out we're gonna put if statements in here i'm gonna say if s um is not no and paste it in there okay one of the things you're going to notice is that there is no warning here there was a warning when it was out here actually let's paste it in here and we'll see that a warning pops up d reference of a possibly null reference because this is potentially null and you can't say dot length of a null value but in here it's not giving us the warning and why is it not giving us the warning because it's smart enough to know oh you did a null check and it can't get into the curly braces unless it is not null therefore this is only going to be if this is not null therefore i'm not going to warn you about that and so this new system allows us to to see when things are null and when things aren't potentially now there's a lot of ways to trick this because the the compiler in visual studio is only so smart it's very smart but it's only so smart and so yes you can trick it but there are ways to get around it if we know actually it's not a problem okay so we're gonna see how to do that as well so how do we do that well let's first kind of trick the system it's not really a trick in the system because um it it really could be null still but we're going to do is we're going to say s equals console.readline now console.reline can give you back a nullable string so it reads the next characters from the input stream or null if no more lines are available now we're going to give it a value okay but this could be null okay so i'm not really tricking the system i'm it's actually telling me the right thing which is hey s could be null but let's pretend in this case that that i'm not tricking the system that it really um that it really isn't going to be null so what i can do is i can use the null forgiving operator and that is the exclamation point so after s i put an exclamation point and i say hey no i know you think this is null but it's not null therefore go ahead and don't give me a warning for this that's all this is doing this is just saying don't warn me on this when it comes to your actual compiled code this goes away it's not doing anything else it's just for kind of in design time when you're you're compiling your application where it figures out if you have warnings or not that's where it would uh be last used after that kind of gets tossed but this tells the compiler it says hey you know this isn't really um an ever a null value okay and there are times when you know for certain that's not a null value for one instance is when you have um i just came across as recently as i was teaching a course on apin.net six where you would pass in a value that was on the a parameter and you knew for certain that it was not no but because it's a string it could be null and so it's like well i'm not sure if it's null or not and you're like no no no you can't get to this route unless it's not null therefore you can say exclamation point and say you know what that's never going to be null so in this case let's just run this where it's going to pop up a box it's going to ask us for a value let's just put in a test which is four characters and it says four so it knows yep you're good now if i run this and i were to hit enter it gives me zero character because it does give me an actual string but this allows me to kind of ignore that warning if i think it's an invalid warning now again in this case it's not an invalid warning it actually is valid all right so that's how to use the null forgiving operator the exclamation point you put it right after the variable right before you do anything else like dot now let's look at another option here and let's comment these lines out and we're going to look at this person model so let's do this let's say right now it's null we're going to say if and we had let's just check the length of the name now if i were to say p dot name dot length well i think a problem because notice even says right here cannot implicitly convert oh yeah that's a different problem um is greater than zero how about that so then we're going to say uh p dot name dot length show it off okay but we have to check and see it it's great on zero first right well the problem is this is null so it gives us a warning here it says hey you're potentially using a null reference and if i were to run this right now it would have an exception it said no reference exception object reference not set to an instance of an object so it's saying hey p is null therefore you cannot access a property off of a null value now let's just pretend for a minute that i were to say p equals new okay i run again and i get an exception again audit reference not set to an instance of an object so i have p has a value but name is no therefore can't get length off of name so in order to solve that problem i'd have to make sure that p dot name had a value and then it would actually run and it would say the length was 3. okay but if i want to do this right and let's just get rid of these for now if i want to do this right where i check these things we have the ability to use what's called the null conditional operator what that says is i can put a question mark after p and i say hey if it's not null then get the name property but if it is null then just short circuit stop right there and declare this as false okay whatever this check is this is being false so it's not going to do the if statement in fact we'll do an else here we're going to say console writeline this was false all right if we run this no exception it says this was false so this says hey if it's null just exit out and go the else statement because this can't possibly be true because we're whatever comparison we're doing we're trying to compare against a null value and we can't do that therefore it has failed the if check and it stops right there now imagine for a minute we were to do the p equals new well now p is no longer null so but we do know that is names null but again we can put the the null conditional operator after name to check for that as well and if we run this we get this was false so this has a value but this does not it's still null so using the the null conditional operator we can check to make sure that it has a value before we go to the next step down the line so we're saying hey as long as it's not null keep going if it is null exit out all right so that's the null conditional operator now there is another thing here we can play around with and by the way before we go down to that this right here can be written a different way in fact let's let's do that let's copy this i'm going to put it inside quotes here from it i'm going to say it p is not null and p dot name is not null and p dot name dot length is greater than zero that's the equivalent we were checking first to make sure p is not null and remember with an if statement if we have these ands here if the first one evaluates to false it stops right there short circuits and what it does it says okay if it is null then go to the else but if it's not null so we have a true statement for the first thing then go on to the second thing and see if we have a true statement and it's not null well is null therefore we short circuit and go to the bottom but if it's not null this is true then we go to this third one and we know we can say p dot name dot length because both p and name are not null so that's what we used to have to do this is what we do now because of that very easy to use null conditional operator okay so a lot easier to use compared to the the alternative now let's talk next about the null coalescing operator so a null coalescing operator is let's look at this s right here um let's just say we want to have a string um if we use testy i don't think so um or t let's just string t so t equals i want to equal s but i don't want to be null so i want a default value if it's or if s is null i want to say hello world okay and that's what the null coalescing operator does is it's going to use this value to put in a t unless this value is null in which case it's going to use this value so it's saying that t will never be null this way so now if i were to do a console right line here and say um string interpolation the value of t is and we'll do t like so and that's going to print out t right and if i run this it says the value of t is hello world because s was null but let's just say that we had modified s equal to tim corey and that's again the value of t is tim corey and never assigned hello world because the fact that s was not null therefore s went into the value of t as opposed to the hello world if s was null all right so that's the null coalescing operator now there's one that's very very similar to the null coalescing operator let's um boy we've got a lot of stuff going on here let's go up to the very top let's have a list of int and we're going to call it um i don't know numbers and let's say equal no for now all right which by the way is going to um it's going to pro potentially a problem because the fact that let's just check here we're on warnings let's make it enabled this is potentially a problem because the fact that this should not be nullable so it says right here converting a null literal or possible null value to non-nullable type because list is a reference type so we have to say question mark in order to not get to yellows now imagine for a minute that i wanted to put a value into my list so i want to say numbers dot add 8. now the problem is that numbers may be null here and that's a problem now if i know for certain it's not i could do this and use this null forgiving operator but i don't know that because it might be so what do i do to make sure that it's not no well i can wrap it let's wrap it in parentheses okay which wouldn't change anything right now but now i can say if numbers and using null coalescing but null coalescing assignment operator two question marks and equal and then say new and what this does it says hey if you already have a value here and it's not null then don't do anything but if you don't instantiate it as a new list event and then add eight so notice there's no warnings here anymore because this will work now if i were to change the um the numbers equals new like that and say numbers dot add one that will work as well so now we have we'd have a list with one and eight in it because of the fact that it wouldn't this wouldn't do anything since it's it is not null therefore it's not going to do this part of the code all right so this allows us to do a check in line and assign a value as well so that's the null coalescing assignment operator i don't seem to use this very often but i want you to see that it's available all right so that is the null coalescing assignment operator we have covered the null conditional operator the null coalescing operator and we've talked through these operators right here okay the nullable annotation context but we've talked about how we can do enable disable but also warnings and annotations but when you have a massive program i mean this is only one or actually two code files but we have a massive code file that can be overwhelming where you get all these warnings whereas saying hey you've got a problem here you got a problem there and you know all these problems are popping up well what you can do is you can change this to disable or leave it at disable if that's where you're starting from and there's no warnings okay you don't get the warnings from from this but you can start to enable it file by file if you want so at the top of this file i can say nullable and then say enable and then if i wanted to i could come down you know for halfway through i could say nullable uh actually instead of disabling i say restore and that would restore it for anything below it so let's just pretend for a minute that public class address model now i don't like to have more than one class in a file but this is for demonstration purposes so if i were to have you know string street address string city these would all be warnings normally if i were to take this out these are gonna change over to warnings but if if i put that in i'm gonna say you know what i'm just gonna address this stuff and not worry about these warnings yet okay so that's how you can use these directives again this is only for at compile time it's not for at run time this gets tripped out but this will allow you to start to change over slowly to the the more full way of working with with these null values so there are nine different options when it comes to this nullable okay so we've seen enable which that's the equivalent of saying enable here we've seen restore which what that does is it says whatever you have over here is what you're at now and then we also have disable which would be the equivalent of what we have right here which is disable so that'd be disabling it for just this file so that's the three directives but then we have also don't forget about warnings and annotations so we could say enable or nullable enable warnings and then nullable restore warnings like so and we can say nullable enable annotations and nullable restore annotations so this allows us to add warnings or annotations on the end of enable restore or disable all right so those are options here when it comes to uh this this directive so there's a lot of stuff when it comes to knowable in fact what i'm gonna do is i'm going to the very top here since i want to recap we've talked about to make sure that you really understand the different types not just because you need to know how to use them which you do but also because if you know the right name for them you can google them it's a whole lot easier to google when you know the right name especially since microsoft has decided that the question mark character needs to be used in a lot of different contexts and so you you're googling things like c sharp question mark and people are like do you have a problem with c sharp and no i just have a problem knowing what the question mark means so let's talk it through so if i were to say int question mark that is a a nullable value type if i were to say string question mark that's a nullable reference type and if i were to say id question mark dot tostring that is the null conditional operator all right now it's not to be confused with the conditional operator um yes i how is that the same name i don't know but the conditional operator is when you use the question mark um let's just say uh int x equals um and they could say i don't know one equals zero question mark one or zero okay this right here is the conditional operator it has nothing to do with null what this is is the inline if statement so there's your your comparison which of course that's always going to be false but this is says okay it's a this is a an if statement there's our true value colon there's our false value so you put the false value into x so x would be zero because the fact this is always false therefore it would never hit the true value but this is a conditional operator that has nothing to do with null that's not what we're talking about this right here is the null conditional operator it's yes in some way it's kind of similar because it's saying it's kind of a hidden if statement here if this is no then stop right there return to false but if it's not null then go ahead and do this work okay so yes it's kind of i kind of i kind of get it but um yes it's confusing too so i want to make sure i pointed that out as well so then we have um let's just say var v equals i question mark question mark zero this is the null coalescing operator where we're saying hey if i is null then put the value of zero in v but it was not null then put the value of i into v right that's the null coalescing operator and they also have and let's just make sure we um actually can i just take this from right here as our example this is the null no coalescing assignment operator null coalescing assignment operator yeah that's long name what this is is it not only does a null check it checks if this is null that puts this value into the thing it just checked all right that's the difference where this one checks the null but doesn't put this value into i it puts this value into v okay that's the difference all right and then we have and let's go over here and copy it let's turn it back to enable this right here this is the nullable annotation context okay and by default like i said in c sharp i'm sorry in dot net 5 and below this is disabled or not present which is the same but in in net 6 it's going to be enabled by default for new projects that does not mean you have to keep it enabled so if you were to upgrade a project from.net5.net 6 it will be disabled because it was not a new project it was an existing project it got upgraded now you can absolutely turn it on but this is going to um check to see if it's enabled if it's enabled that's going to do all of our checks notice we have our our warnings here um non-nullable name must contain non-null value when acting the constructor the annotation for a null reference type can be used in the code nullable annotation context and the value of that last one is not part of this so that's we can ignore that one um so there's two warnings we have right now because the fact that we've enabled that nullable annotation context now just to be clear on that there are four values for uh for this and that is enable disable warnings and annotations okay those are the four values you can put inside here now we also have the ability to use our directives so we have for example nullable enable this is a nullable directive and we have the options so we have enable disable we have init and we also have restore we also have enable warnings disable we'll just say enable warnings or enable uh annotations and i'm gonna let you fill in the rest here because we don't need to type all those out but it would be um disable warnings disable annotations it'd be restore warnings restore annotations and of course that's all in front of nullable so it would be nullable enable warnings hashtag nullable enable annotations hashtag nullabledisable and so on for all different all the nine different options now we're not done yet we also have let's just do s exclamation point to string that is the null forgiving operator so what that does is it says hey i know you think this is null or potentially null but i know for certain it's not therefore use that to tell a compiler don't give me a warning for this because i know it's not no okay use that with caution because remember if you use that incorrectly it can bite you because it could be null all right so um let me just demonstrate that real quick here because i think it's important let's um i got a lot of stuff going here um let's come up here and [Music] just do string test equals no and i were to say um console.writeline test.length okay and it's going to warn me that that's a problem all right so i can't do a length on test right it should have warned me um yep let's hit save here so there we go there's no warnings uh dereference of possibly null reference okay and if i were to run this that'd be a well let's first say hey you know what i know for sure that's not null now i'm lying it's not it is null let's run this it gives me an exception audit reference not set an instance of an object it doesn't matter that i told it no this is not null because it absolutely is so if i lie then i'll get burned so don't lie all right don't lie and say something is not ever null when it potentially could be so be use that very very carefully make sure that you know for a fact that it's not going to be null all right so let's um i'll leave that in there for you i'll comment it out because i don't like to have exceptions in my code okay so that's a null forgiving operator now we can use this in not just in our inline code we can also use it in methods so for instance our return types can be marked as nullable our parameters can be marked as nullable we can even mark our our generics as nullable or not nullable but i tell you what that gets into a whole lot of um deeper more complicated code okay here's what i'll do i will bring this up i'm going to copy this i'm going to give you a i'm going to put it in the code so i'll put it up here no with generics there is the link okay because in let's look at here in um i'll zoom in a little bit there we go so in c sharp 8.0 you could not use the t question mark for nullable t without it being constrained to a struct or clast however in c sharp 9 that restriction was removed and then here are the criteria for what a a nullable t means and then it lists the criteria and then it lists the return values and what that might be and based upon different behaviors and so on that gets a lot farther in the weeds and i want to go in this video but i do want you know that yes you can do it and if you do you can see what those consequences might be for your application but that way you can be a little more clear as to whether or not that generic can be null all right so there's a lot to dive into there i'll let you read through that on your own those rules are there and they're pretty well explained but i think it's probably better to read those rules when you need to use a generic that's nullable because then you you can see the practicality of your specific situation rather than trying to have me go over it and then just kind of going in one ear and out the other because you're not using it for a while so that is how we work with null inside of c sharp there's a lot here there's a lot here and we didn't even call the fact that you know for instance in here um notice how young about the name well if i use for instance dapper where i'm pulling this from a database and this model is going to be populated from the database well i'm not going to have a constructor because what this really wants is it wants you to create a constructor and then notice that the warning went way up here and brought down here it says hey you must take care of name in this constructor so name has to equal a value in order for before we end the constructor otherwise there's a problem maybe it might be a null value however if i use this model with dapper it instantiates it using the null constru the empty constructor and that doesn't assign a value to name and i don't want to assign an initial value i don't want to say um equals you know string dot empty because that's not really necessary that's that could be a solution that could be a solution that works for you and if that's what you have to get the warning go away that might be the right thing um but really this is an this is supposed to be a actual value not an empty string an actual value in name so i'm not a huge fan of that so what i might do here is say disable so null will disable and and say you know what ignore these ignore them because the fact that i know that this in a database is does not allow a null value so if i'm getting the values from a database it cannot be null therefore i know there should never be no when i'm getting values from a database and maybe i have a separate constructor that creates this when you're creating the person model well then i would make sure that you know it that it is done properly but you can go different ways down this path and notice there there will be times when it's a little frustrating when you have warnings that you don't think are valid but you can wrap them if you want to do something like this or you can do a string.empty because that really is kind of valid um in most cases it's not for the create statement but but otherwise it's valid for getting data from the database so it's up to you how you want to handle it but while this does add a little bit of pain in some places where it gives us more warnings than we kind of wanted it also gives us a lot more protection when it comes to doing things like this where forget the um the exclamation point here but if if we say test.length we forget about the fact that tests could be null then we can get an exception at run time and so by having that warning there saying hey you know what you could be getting exceptions right now that could be a problem in your code well that allows me to go oh i need to take care of that and you know do the um you know whatever i want to do whether it be say null coalescing or what i want to say um the you know put the question mark after it's like this the null conditional operator you know whatever i want to do but at least i've now thought through this could be null and that's what this uh system allows for so a noble annotation context is a good thing it just takes some getting up to speed and it takes some work to work around these null values now uh the the person who created the null way back when said that the null was a billion dollar mistake and i can see both sides of that there are reasons i think why null is valid and there's some that i understand where null is could not be valid um but whether or not it's valid without a mistake we have to deal with null null is a reality of pretty much every programming language out there and so this system allows us to put in place some safeguards to make sure that while we're dealing with null we have the most protection possible these warnings here pop up to tell us hey you could have some issues in your code okay so either market is nullable meaning you expect it to be null or make sure that you put a value in it right away so this is a good system and yes there's a lot of different things to do to work with nulls but i think it's important to do because it does elevate your game it makes sure that you have less potential bugs in your code that go to production all right so that's how to work with null in c sharp i am curious as to how many of these different ways to work with null you were not familiar with let me know down in the comments let me know which ones you know popped up as as new things to you for example the the null forgiving operator that's pretty new um you know have you used it before have you used the different values for um for your nullable annotation context you know not as enabling this disabled but also the warnings and annotations have you used the inline the the null coalescing assignment operator love that name have you used that i was curious as to how much you've worked with these different uh systems and different operators in working with null in c sharp i'd love to know let me know down in the comments if you have any other questions about null or if you have other scenarios you've run into where you're not quite sure how to deal with null in that scenario all right thanks for watching as always i am tim corey [Music] [Applause] you
Info
Channel: IAmTimCorey
Views: 34,359
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# course, C# training, C# tutorial, .net core, vs2022, .net 6, c# null
Id: -bn4e5xUEeM
Channel Id: undefined
Length: 57min 25sec (3445 seconds)
Published: Tue Jul 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.