Welcome back.
In this video we're going to look at nobles.
And nobles are a really powerful tool.
That was added in C sharp a few years ago and is something that I came across the first time when I
used Kotlin, which is a newer programming language, so to speak, and it had this feature baked in,
I think, from the start.
So nobles are basically variables that can be null.
Okay.
Because normal variables in C sharp they cannot be null.
Let's have a look at it.
So if I have this int age for example, and I assign null to it, I'm going to get a compiler error.
So it directly says hey this doesn't work.
You cannot do it because you cannot convert null to int because it's a non-nullable value type.
So it is a Non-nullable value type.
How can I make it a nullable value type?
Well, I can just add a question mark after the int keyword.
So now this becomes a nullable value type.
Now this becomes a nullable int.
So int question mark is a nullable int nullable in the sense that it is able to be null.
That's how you can memorize it.
So it's nullable.
Now I can make it null.
Let's have it check whether it has a value or not and move forward based on that.
So let's say age has a value.
Then I would write out to the console age is colon and then age dot value.
So what you see here is when you're working with nullable you you can access this value property.
The same goes for has value.
So this has value will return whether the variable that or the nullable that we're applying it onto
is null or not.
So if it is null then it has value will return false.
And if it does have a value, it will return true.
So that's why this if statement works here.
And then this age value will give us the actual value of that age.
Okay.
Now in the else block, what I'm going to do is I'm just going to write age is not specified okay.
So now in our case it's going to say age is not specified because age is null and therefore this is
not running.
So it says this is false.
So it goes into the else block straight away.
Now you can use age directly.
It will not cause an error straight away.
So you can use for example here plus h.
This will not cause an error interestingly enough.
And that is because of the type safety in C sharp.
So C sharp is quite smart about it.
It just says okay well you know what?
I'm going to ignore it.
But if you are trying to do something where let's say age does not have a value.
So we know that in the else block age is not specified.
So we could not go ahead.
And let's say we have another variable int my age is equal to 35.
For example, I cannot say in some is going to be age plus my age.
Okay, this won't work.
So we need to use h dot value to go ahead and use it.
But h dot value will cause an error if h does not have a value.
So if h is actually null okay, let's run this code and let's see which error we're going to get.
So you see we get this system invalid operation exception nullable object must have a value is the error
that I'm getting.
We can only access h dot value if we are using has value.
And we can only add a nullable to a non-nullable if we actually access the value itself rather than
the variable.
So as you saw, we cannot just say h plus my age because it says cannot cannot implicitly convert int
nullable to int, an explicit conversion exists.
Are you missing a cast?
And then it says nullable value type may be null.
So this may be null which is why it says check whether it has a value which we did with this if statement,
and only then do this calculation.
So if we were to let's say here instead of has value we say value.
And we want to do this calculation, we could do it in here.
Here it would work because we can access value as has value has already checked that h is actually not
null.
So what we're just saying is you can't add nothingness with something else.
You have to make sure that nothingness is not nothingness.
So it has some like it is actually a value.
Okay, so let's look at usage in real world scenarios because it is a little weird, right?
What is this nullable all about?
Well, the thing is, nullable types are particularly useful in scenarios where a variable may not be
applicable.
Or where values are missing, for example in database interactions.
So database fields may return null if they are not set.
So let's say we have a database with the username, the email address, the phone number and the address
of the user.
But the user never entered their telephone number.
So suddenly we have a null field there and we cannot handle it otherwise unless we are capable of actually
handling nulls or nullable and nullable.
These are perfect for handling such cases.
All right.
And then form data.
So when dealing with forms where users may not provide all fields like optional survey responses, nullable
types can indicate whether a response was given.
So this is again pretty much similar to the database example that I just gave you.
But it's the same idea.
Basically we don't have the data, so we just don't get the data from a database or from the user or
whatever.
And if we don't get it and we don't handle it properly, then our application will crash.
And that's something we want to avoid.
So what would be the benefits of using nullable types.
Well first of all safety.
So it prevents runtime errors that can occur when dealing with types that may not have values like we
just saw in the database examples in the form example as well.
And then clarity.
So it makes clear in your code that a variable might not always have a value which improves the code
readability and maintainability.
And sometimes you will see that a method is expecting a nullable.
So whenever we're working with methods that require nullable, it just makes sense to use them as well.
So we're going to see or we already saw a few examples where the method was expecting a nullable, but
it was also fine having a non-nullable as the argument.
So that's something that I will show you more of or go deeper into each time that we come across it
in the next videos.