They were introduced in version 6, so bring the slide on out and let's get
started. So to start with the problem is that in most programming languages the
programmer needs to check for null pointers or null references before
reference the variable or access one of its properties or methods then that
certainly adds to code complexity. So let's assume that we have a method that
returns a reference in this case a string it is possible that the method
could return a null now this method that I've created is called get some
even seconds we're gonna return null and on odd seconds we're going to return a
string that contains the text the current time is and the time now this
also contains an interpolated string which is another one of our learning
short videos so you might want to take a look at that as well so the wrong way to
access this method is to simply call it and not check to see if the result is
null or not in this example the programmer assumes that data contains a
string and they display that data they also display the hash code
of that string now the data inside the curly braces here does not produce a
syntax error the interpolated string simply ignores the null however data dot
get hash code is going to throw a null reference exception now the right way or
the old way to do it correctly the programmer needs to check for null so in
this example we're assigning the data variable with the result of calling get
some information and we're checking for null before we display the current time
and the hash code now this introduces a couple of smells one of them is having a
transient variable it also contains an if statement the new way as of c-sharp
version six is to use the null operator that operator is question mark dot so
rather than using just a dot to access a property or method we're going to use
question mark dot if the left side is null then the result is null if the left
side is not null then the method or property will be accessed so as you can
see in this example we're using question mark dot to call the method get hash
code if get some information returns null then the question mark results in a
null if the call to get some information returns null then get hash code is not
invoked and the result of the overall expression is then no if get some
information actually returns a string then we will invoke a get hash code and
what to displayed is the hash code the integer value another operator is known
as the coalescing null operator this is the double question mark operator it
allows the code to specify a default value if the left side of the dot is
null it can be used standalone as you see in this example where we call get
some information question mark question mark def so if get some information
returns null then nope time provided is the result if gets some information
doesn't return null then object is the result it can also be used
with the question mark dot so following the question mark dot again we are not
going to call get hash code if the left hand side is null instead we'll use
minus 1 if the left hand side returns an object then get hash code will be called
and the integer result is whatever get hash code returns
all right so in conclusion the null operators and the coalescing null
operator are a great way for us to avoid null reference exceptions and to also
avoid unnecessary if statements and transient variables we also saw
interpolated strings in this video but that's the subject of another learn it
short video hey follow us on social media or check out our longer courses on
our website and you can find the links to all of that below I'm Chuck McCullen
as always code heavy