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 - C# Tutorial
From the course: C# Essential Training 1: Types and Control Flow
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
Defining enums4m 39s
-
(Locked)
Enabling multiple enum values4m 3s
-
(Locked)
Testing enum values4m 13s
-
(Locked)
Understanding nullable types3m 24s
-
(Locked)
Using nullable types2m 14s
-
(Locked)
Working with null objects3m 25s
-
(Locked)
Understanding nullable reference types4m 23s
-
(Locked)
Working with nullable reference types4m 8s
-
(Locked)
Solution: Enum Flags1m 42s
-
(Locked)
-
-