May 30, 2016
As programmers we are used to checking a variable value for null before we use it in a subsequent block of code. It's one of the most repetitive tasks we do every day. In today's blog I will introduce you to the new question mark dot operator introduced in C# 6.0. You will be able to complete this repetitive null checking task in an easier way.
Let's say you have a class Product with a property Title that could be null if was not yet initialised. Now let's say you have a block of code inside a method of the class Product that only makes sense if the Title of the product is not an empty string. One way to check it is to check if the Length of the Title is bigger than zero. The problem is that I can only get the Length if the Title is not itself a null. So the usual way to write the conditional is as follows:
if ( this.Title != null
& & this.Title.Length > 0 )
{
//here I can safely use the Title...
}
If I just check whether or not this.Title.Length > 0 I will get an exception whenever the property Title was not initialised. So we do need to include the this.Title != null part before looking into the Length. This pattern of checking if something is not null before retrieving the value of a property of it is prevalent and leads to the tedious null checking condition appearing everywhere.
The question mark dot operator addresses this issue in an elegant way as you can see in the following code snippet.
if ( this.Title?.Length > 0 )
{ ... }
On the left of ?. we have something that could be null or not, but when it is not null we have on the right of the operator a property we want to evaluate. If the left hand side was a null the Length is never evaluated and the conditional automatically evaluates to false. So no exceptions are raised if the Title is not initialised, we simply do not execute the block of code in the body of the if statement.
Another very useful application of the ?. operator is to check if there is a listener to an event before triggering the event. The following code snippet illustrates this usage.
OnSaveProduct?.Invoke(this, args);
The method ,em>Invoke is only called if the delegate OnSaveProduct is not null.
The ?. operator is a recent addition to C#, but it is worth to remind you of the null-coalescing operator ?? that has been available since Visual Studio 2010. The next code snippet shows the ?? operator in action.
Console.WriteLine( product.Title ?? "NA" );If the Title is not null we display its value, otherwise we display NA.
To learn more about C# we recommend the course 20483: Programming in C#.
How do your Excel skills stack up?
Test NowNext up:
- It isn’t the changes that do you in, it’s the transition
- Lies, Damned Lies, and Statistics
- Service Transition in ITIL®
- Finding just the working days in Excel
- Microsoft Azure Databases
- PowerShell Basics Series– Measuring Objects (Part 1)
- Managing poor performers when counselling doesn’t help
- 4 points to consider when creating an application
- Business Connectivity Services in SharePoint
- Beguile for a while with a smile
Previously
- Managing Employees through Frequent Casual Feedback
- 4 TED Talks to help you overcome stress
- What is a PCI-Express Lane?
- InDesign CC - Colour Theme Tool
- How-to: Make dates and currency in Excel show properly in a Word Mail Merge (Part 1)
- PowerShell Basics Series - Sorting and Selecting
- Course Divorce
- InDesign CC - Creative Cloud Libraries
- What to expect from SQL Server 2016
- Every Microsoft Word Keyboard Shortcut