Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

??: practical reference

??: practical reference: ?? returns its left operand when non-null and otherwise lazily evaluates the right operand.

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

Syntax

left ?? right

?? returns its left operand when non-null and otherwise lazily evaluates the right operand.

Use it for defaults without rejecting false, zero, or an empty string.

The right side may not execute, so it must not contain a required effect.

The operator associates to the right; the left operand may be a reference type, nullable value type, or unconstrained type parameter, but not a known non-nullable value type.

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

Examples

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

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

Result

fallback

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

Sources