ALERT

[WEBINAR] See the Whole Story: The Case for a Visualization Platform

Null-Coalescing Operator

Definition - What does Null-Coalescing Operator mean?

A null coalescing operator, in C#, is an operator that is used to check whether the value of a variable is null. It is represented by the symbol "??".

The null coalescing operator allows for the selection of the first non-null value from a pair of values. It is used to set the default value for variables of nullable value type or reference type. It can be used to build an expression with simplified syntax that is logically equivalent to an expression using an if statement or ternary operator in a more compact form. The expression containing the null-coalescing operator has minimal source code and provides better readability.

In applications such as those related to database and XML data, variables can occur in an undefined state, which implies that they are not set to any proper value. A null-coalescing operator is used to check such a variable (of nullable type) for null. If the variable is null, the null-coalescing operator is used to supply the default value while assigning to a variable of non-nullable type.

Techopedia explains Null-Coalescing Operator

The null-coalescing operator is a binary operator that is used in a conditional expression of the form, “a??b”, where the expression in the left-hand operand, "a", must be nullable type or reference type. If "a" is not evaluated as null, it returns the result of the evaluation of "a". Otherwise, the expression in the right-hand operand, "b", is evaluated to obtain the result of the entire expression.

For example, the null-coalescing operator can be used in a property of an object for returning a custom default value to avoid returning a null value.

The assignment of a nullable value type variable to a non-nullable type results in a compiler error, and the use of explicit cast for such an assignment can result in an exception. A null-coalescing operator is used in such assignments to avoid the compiler error and the exception.

The operands used in the expression containing the null-coalescing operator must evaluate to a matching type or should be implicitly converted to a common type.

The operations in an expression that uses the null-coalescing operator are grouped from right to left.

This definition was written in the context of C#

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
Tweat cdn.techopedia.com
"Techopedia" on Twitter


'@Techopedia'
Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join thousands of others with our weekly newsletter

Resources
Free Whitepaper: The Path to Hybrid Cloud
Free Whitepaper: The Path to Hybrid Cloud:
The Path to Hybrid Cloud: Intelligent Bursting To Amazon Web Services & Microsoft Azure
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.