C# tutorials > Modern C# Features > C# 6.0 and Later > What is the null-coalescing assignment operator (`??=`)?
What is the null-coalescing assignment operator (`??=`)?
The null-coalescing assignment operator (??=
) is a concise way to assign a value to a variable if, and only if, that variable is currently null
. It's a shorthand for a common null-check-and-assign pattern, making your code cleaner and more readable.
Basic Syntax and Usage
The In the example, ??=
operator checks if the variable on the left-hand side is null
. If it is, it assigns the value on the right-hand side to the variable. If it's not null
, the variable's value remains unchanged.name
is initially null
, so it's assigned "John Doe". existingName
is already "Alice", so it remains unchanged even after the ??=
operation.
string name = null;
name ??= "John Doe";
Console.WriteLine(name); // Output: John Doe
string existingName = "Alice";
existingName ??= "Jane Doe";
Console.WriteLine(existingName); // Output: Alice
Concepts Behind the Snippet
The core concept is to avoid redundant code for null checks. Without The ??=
, you'd need to write an if
statement to achieve the same result:if (name == null) { name = "John Doe"; }
??=
operator streamlines this process, making the code more compact and easier to understand at a glance. It also promotes immutability by only assigning a value when strictly necessary.
Real-Life Use Case
Consider a scenario where you're retrieving configuration settings. If a setting is not found (resulting in a This ensures that null
value), you want to provide a default value:string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
connectionString ??= "DefaultConnectionString";
connectionString
always has a valid value, even if the application configuration is missing the "ConnectionString" setting.
Best Practices
Use judiciously: Only use Clarity is key: While concise, ensure that the logic remains clear. If the default value calculation is complex, consider using a separate Consider thread safety: When working with multi-threaded environments, be mindful of race conditions. While ??=
when you explicitly want to assign a default value if the variable is null
. Don't use it as a general-purpose assignment operator.if
statement for better readability.??=
itself is atomic in its operation, the code leading up to it might not be. Use locks or other synchronization mechanisms if necessary.
Interview Tip
When discussing the ??=
operator, highlight its role in improving code readability and reducing boilerplate. Mention its use in providing default values and its relationship to the null-coalescing operator (??
). Be prepared to explain scenarios where its use is beneficial and scenarios where a more explicit if
statement might be preferred for clarity.
When to Use Them
Use Examples:??=
when you want to conditionally assign a value to a variable only if it's currently null
.
null
.
Alternatives
The primary alternative to While longer, this approach might be more readable for complex scenarios. You could also use the null-coalescing operator ( This achieves the same result as ??=
is the traditional if
statement:if (variable == null) { variable = defaultValue; }
??
) combined with a standard assignment:variable = variable ?? defaultValue;
??=
but is slightly less concise.
Pros
Cons
FAQ
-
Is `??=` the same as `??`?
No.
??
is the null-coalescing operator, which returns the left-hand operand if it's notnull
, otherwise it returns the right-hand operand.??=
is the null-coalescing assignment operator, which assigns the right-hand operand to the left-hand operand only if the left-hand operand isnull
. -
Can I use `??=` with value types?
Yes, but only if the value type is nullable (e.g.,
int?
,DateTime?
). Value types themselves cannot benull
; only nullable value types can. -
Is `??=` thread-safe?
The operator itself is atomic, but the overall operation might not be. If multiple threads could potentially access and modify the variable concurrently, you'll need to use synchronization mechanisms (e.g., locks) to ensure thread safety.