Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What does the two question marks together (??) mean in C#?
The null-coalescing operator (??) in C# returns the value of its left-hand operand if it isn't null; otherwise, it evaluates and returns the right-hand operand. This operator provides a concise way to handle null values and assign default values.
The ?? operator is particularly useful with nullable types, which can represent either a value from the type's domain or be undefined (null). It helps prevent InvalidOperationException exceptions and reduces the need for verbose null-checking code.
Syntax
Following is the syntax for the null-coalescing operator −
result = leftOperand ?? rightOperand;
If leftOperand is not null, result gets the value of leftOperand. If leftOperand is null, result gets the value of rightOperand.
How It Works
Using Null-Coalescing with Nullable Value Types
Example
using System;
class Program {
static void Main(string[] args) {
int? value1 = null;
int value2 = value1 ?? 99;
Console.WriteLine("Value2: " + value2);
int? value3 = 42;
int value4 = value3 ?? 99;
Console.WriteLine("Value4: " + value4);
// Multiple null-coalescing operators
int? a = null;
int? b = null;
int? c = 100;
int result = a ?? b ?? c ?? 0;
Console.WriteLine("Result: " + result);
}
}
The output of the above code is −
Value2: 99 Value4: 42 Result: 100
Using Null-Coalescing with Reference Types
Example
using System;
class Program {
static void Main(string[] args) {
string testString = "Null Coalescing";
string resultString = testString ?? "Original string is null";
Console.WriteLine("Result: " + resultString);
string nullString = null;
string defaultString = nullString ?? "Default value";
Console.WriteLine("Default: " + defaultString);
// With arrays
int[] numbers = null;
int[] result = numbers ?? new int[] { 1, 2, 3 };
Console.WriteLine("Array length: " + result.Length);
}
}
The output of the above code is −
Result: Null Coalescing Default: Default value Array length: 3
Advantages of Null-Coalescing Operator
-
It is used to define a default value for a nullable item (for both value types and reference types).
-
It prevents the runtime
InvalidOperationExceptionexception. -
It helps us to remove many redundant "if" conditions.
-
It works for both reference types and value types.
-
The code becomes well-organized and readable.
Comparison with Traditional Null Checking
| Traditional Approach | Using ?? Operator |
|---|---|
| if (value != null) return value; else return defaultValue; |
return value ?? defaultValue; |
| More verbose, multiple lines | Concise, single expression |
| Requires explicit null checking | Automatic null checking |
Conclusion
The null-coalescing operator (??) in C# provides a clean and efficient way to handle null values by returning a default value when the left operand is null. It simplifies code by eliminating verbose null-checking statements and works with both nullable value types and reference types.
