Null-Conditional Operators

One of the most repetitive tasks you do as a programmer is to check a value for null before you work with it. The code to do this checking is typically all over your application. For example, the following verifies whether properties on an object are null before working with them. (For a more complete discussion of all operators, see the section “Understanding Operators” later in this chapter.)

C#

public bool IsValid(){    if (this.Name != null &&        this.Name.Length > 0 &&        this.EmpId != null &&        this.EmpId.Length > 0)    {        return true;    }        else    {        return false;    }}

VB

Public Function IsValid() As Boolean    If Me.Name ...

Get Microsoft Visual Studio 2015 Unleashed, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.