From the course: C# Essential Training 1: Types and Control Flow

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Working with null objects

Working with null objects

- [Instructor] There are a couple of really useful operators when you're working with nullable types, whether they're reference types or value types. I'm just going to clean up a little bit here, get some things out of the way. Let's say we've got our definiteInt up here, and that, remember, is an integer. So if I try and set that equal to age, let's just pretend we're going to end that statement. It doesn't like that, because age could be null, and definiteInt has to be an int. So we can't do an assignment here. It doesn't automatically, for example, convert age to zero. But what we could do, is we can say, well, if age is null, then I want to assign 17 into definiteInt. So these two question marks together, the null coalescing, evaluates the left side. It says, are you null? In this case, if age is null, it says, well, what's on the right side? 17? Okay. That's the result of this expression. So definiteInt would be 17. We can take this, copy it, put it up here, and instead of age…

Contents