Null checking is a constant duty for .NET developers. In this tip, look at the C# null-propagation operator and how it can simplify null checks for nest call chains.
- [Instructor] How many times have you run into an unexpected null reference exception when you're working in .NET, or C# or Visual Basic? It happens more often than it should, at least it does in my case. So the tip that I want to look at today is a language feature that debuted in C# 6 called the null propagation operator. And here's how it works. Here's the situation you use it in. On line 16, I have a variable of type tour, that's set to null, and then on line 18, I have a string variable. What I'd like to do on line 26 is get the tourName property and store that in this string variable.
Because tour1 is null, this line of code will throw an exception. We typically will write code like this to protect ourselves. We'll write a no-statement that says, if tour1 equal equal the null, then return. That's sometimes called a guard clause. Using the null propagation operator, we can reduce some of that. So first let me comment this out, Control + K, Control + C. Instead, what we'll do down here is, we'll use the null propagation operator, which is the question mark and the period.
So this says, if this is null, don't throw the exception. Instead, just store a null value in this variable. So now that I've got a null value stored there, then I can do something like this. I can call a WriteLine, and I'm using a string here, and I'm injecting the potential tourName here. What I'd like to have, of course, is the real tourName there, but since there's a null in this string, nothing will happen. The Console.WriteLine won't cause any trouble when there's a null inside this string expression.
I'll show you want I mean. It just doesn't print anything in that location. So that's the basics of the null propagation. Where this gets more powerful is when you've got a invocation chain, where you've got a lot of operations. You're calling methods that are returning properties, and any of those items in the invocation chain could be null. This simplifies your life. Here's an example I use. I'm instantiating a tour here on line 34, but I'm not setting any of its properties.
Now, let's take a look at the properties. We've got, the tour class has a TourName property, and it also has an IList of TourStops. And there'll be instances of these TourStops somewhere in my code. So here you see, I've declared an instance of the tour, but I haven't set any of the properties, and on tour3, I'm setting everything. Now if I was to try to use this Print TourStop and print out the results, when it's something like this, there's many different places where there could be a null reference.
So I end up having a lot of guard clauses. I check for tour being null, I check for TourName being null, I check for TourStops being null, I check for the count being greater than zero. And so there's a lot of cruft around my code, where this is the real code that's actually doing the work. So again, we'll try to reduce that with the null propagation operator, like this. I used the null propagation operator here. Remember, if this is null, then it's just going to leave that blank. Here, I'm saying the same thing for tour.
Also, if TourStops is null, the collection is null, and if the result from FirstOrDefault is null also. So we'll just set one line of code now. And I have a breakpoint in here. Remove that. Start ... Press the enter key ... And you see that in tour2, I got the tour name, but everything else was left out.
On the tour3, it printed everything. This is a nice feature. It's useful in some scenarios. I'm sure you've got some ideas how to use it in your project.
Skill Level Intermediate
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.