Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

??=: practical reference

??=: practical reference: ??= assigns the right value only when the left location is null.

Version: C# operator reference; verified against C# 14

Syntax

left ??= right

??= assigns the right value only when the left location is null.

Use it for lazy initialization of a field or indexer element.

It does not make concurrent initialization safe; two threads can create values.

The left side is evaluated once, and its setter runs only for null.

The complete program actually uses ??=; its exact expected stdout is set.

Examples

using System;
using System.Linq;
using System.Threading.Tasks;

string? value = null; value ??= "set"; Console.WriteLine(value);

Result

set

The complete program actually uses `??=`; its exact expected stdout is `set`.

Sources