s

C# ?? and ??= operators edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 04 September 2020 | 2604

?? Operator

?? is a C# Null-Collation operator is a binary operator that simplifies checking for null values. It returns the left-hand operand if the operand is not null, otherwise it returns the right-hand operand.

String Test1 = null;
String retval1 = Test1 ?? "World";
Console.WriteLine(retval1);

This will give the ouput "World" Since the value of Test1 is null


String Test2 = "Murugan";
String retval2 = Test2 ?? "World";
Console.WriteLine(retval2);

This will give the ouput "Murugan" Since the value of Test2 is not null

??= Operator

??= is a null-coalescing assignment operator that assigns the value of its right-hand operand to its left-hand operand if and only if the left-hand operand evaluates to null. it doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.