From the course: C# Best Practices for Developers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Checking for null

Checking for null - C# Tutorial

From the course: C# Best Practices for Developers

Start my 1-month free trial

Checking for null

- [Instructor] One of the first lessons I've learned when developing, and I remember my mentor was telling me this with a very deep voice, and there should have been dramatic music playing in the background because he sounded so serious. And it was to always, always, always check for null. Always. And I've always remembered that. And when we've had to check for null, we've had to write a little bit of extra syntax, but now we have the Null Conditional Operator. And let me show you exactly what I mean. You may have noticed that in our Actor Class, there were a couple of areas, such as over here, where we are using this question mark. But what's happening is that it's checking to see if value is null, and if it is, it's not going to try to invoke Trim. Now what's nice about that is that it prevents the code from just blowing up on you, from a system exception error happening. And let me go ahead and demonstrate what I'm talking about. I'll hop on over to our program file under…

Contents