Null Operators in C#

Introduction

In this article, we will learn different ways to handle Null properties with the help of different NULL operators available in C#.

Different NULL operators which we will discuss in this article are,

  1. Ternary Operator
  2. NULL Forgiving Operator
  3. NULL Conditional Operator
  4. NULL Coalescing Operator
  5. NULL Coalescing Assignment Operator

Let's discuss all the above operators one by one in detail and with the help of code snippets.

Ternary Operator(?:)

This operator actually evaluates a Boolean expression and returns the result of one of the two expressions, depending on whether the Boolean expression evaluates to true or false,

The syntax for the ternary conditional operator.

condition? consequence: alternative

Just to remember, we can also say if this condition is true. do this: else do this.

Let's try to understand using the code snippet.

//Ternary operator sample
int number = 8;
string ternaryMessage = number % 2 == 0 ? "Number is even" : "Number is odd";

Console.WriteLine(ternaryMessage); // Output: "Number is even"

NULL Forgiving Operator(!)

This operator is used to suppress all nullable warnings for the preceding expression. The unary prefix ! operator is the logical negation operator. The null-forgiving operator has no effect at run time. It only affects the compiler's static flow analysis by changing the null state of the expression.

Let's try to understand using the code snippet.

//Null Forgiving Operator sample
string firstName = null; // Gettting warning
string lastName = firstName!; // Not getting any warning

NULL Conditional Operator(?.)

The Null conditional operators give us a shortened syntax for checking for a null object before calling a method, reading a property, indexing into an object, or accessing another member on a nullable object.

Let's try to understand using the code snippet.

//Null Conditional Operator sample

string firstName = null!;
int? length = firstName?.Length;

Console.WriteLine("Length is " + length); // Code will not break at runtime.

NULL Coalescing Operator(??)

The Null-coalescing operator ?? returns the value of its left-hand operand if it isn't; otherwise, it evaluates the right-hand operand and returns its result. If the left-hand operand evaluates to non-null, the?? operator doesn't evaluate its right-hand operand. The null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. The ??= operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.

Let's try to understand using the code snippet.

//Null Coalescing Operator sample

string firstName = null!;
string name = firstName ?? "DefaultName";
string secondName = name ?? "SecondDefaultName";

Console.WriteLine(name); // Output : "DefaultName"
Console.WriteLine(secondName); // Output : "DefaultName"

In the above example, the firstname is Null, so the name property is assigned a value "DefaultName". Now the name is not null, and secondName will be assigned as the value of the name.

NULL Coalescing Assignment Operator(??=)

This operator was introduced in C# 8.0, which is used to assign the value of its right-hand operand to its left-hand operand only if the value of the left-hand operand is null. If the left-hand evaluates to non-null, then this operator does not evaluate its right-hand operand.

Let's try to understand using the code snippet how we use to work without this operator earlier and how it changes the way of coding now.

// This is how we use to do earlier
string firstName = null!;
if (firstName is null)
{
    firstName = "Name";
}

Console.WriteLine(firstName); // Output : "Name"

Now we can do the above code in a new way by using the Null coalescing operator as per below code snippet.

string firstName = null!;
firstName ??= "Name";

Console.WriteLine(firstName); // Output : "Name"

Summary

I hope this article will help you to understand different ways of handling null values and assignments and how helpful it is nowadays. Please let me know if you have any feedback or suggestions in the comment box.

Thanks, everyone, for taking the time to read this document. Happy coding


Similar Articles