?? and ??= operators - the null-coalescing operators
Članak
The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null; otherwise, it evaluates the right-hand operand and returns its result. The ?? operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null. The null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. The ??= operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.
C#
List<int>? numbers = null;
int? a = null;
Console.WriteLine((numbers isnull)); // expected: true// if numbers is null, initialize it. Then, add 5 to numbers
(numbers ??= new List<int>()).Add(5);
Console.WriteLine(string.Join(" ", numbers)); // output: 5
Console.WriteLine((numbers isnull)); // expected: false
Console.WriteLine((a isnull)); // expected: true
Console.WriteLine((a ?? 3)); // expected: 3 since a is still null // if a is null then assign 0 to a and add a to the list
numbers.Add(a ??= 0);
Console.WriteLine((a isnull)); // expected: false
Console.WriteLine(string.Join(" ", numbers)); // output: 5 0
Console.WriteLine(a); // output: 0
The left-hand operand of the ??= operator must be a variable, a property, or an indexer element.
The type of the left-hand operand of the ?? and ??= operators can't be a non-nullable value type. In particular, you can use the null-coalescing operators with unconstrained type parameters:
C#
privatestaticvoid Display<T>(T a, T backup)
{
Console.WriteLine(a ?? backup);
}
The null-coalescing operators are right-associative. That is, expressions of the form
C#
a ?? b ?? c
d ??= e ??= f
are evaluated as
C#
a ?? (b ?? c)
d ??= (e ??= f)
Examples
The ?? and ??= operators can be useful in the following scenarios:
In expressions with the null-conditional operators ?. and ?[], you can use the ?? operator to provide an alternative expression to evaluate in case the result of the expression with null-conditional operations is null:
C#
doubleSumNumbers(List<double[]> setsOfNumbers, int indexOfSetToSum)
{
return setsOfNumbers?[indexOfSetToSum]?.Sum() ?? double.NaN;
}
var sum = SumNumbers(null, 0);
Console.WriteLine(sum); // output: NaN
When you work with nullable value types and need to provide a value of an underlying value type, use the ?? operator to specify the value to provide in case a nullable type value is null:
C#
int? a = null;
int b = a ?? -1;
Console.WriteLine(b); // output: -1
Use the Nullable<T>.GetValueOrDefault() method if the value to be used when a nullable type value is null should be the default value of the underlying value type.
You can use a throw expression as the right-hand operand of the ?? operator to make the argument-checking code more concise:
C#
publicstring Name
{
get => name;
set => name = value ?? thrownew ArgumentNullException(nameof(value), "Name cannot be null");
}
Izvor za ovaj sadržaj možete pronaći na GitHubu, gdje možete stvarati i pregledavati probleme i zahtjeve za povlačenjem. Dodatne informacije potražite u našem vodiču za suradnike.
Povratne informacije o proizvodu .NET
.NET je projekt otvorenog koda. Odaberite vezu za slanje povratnih informacija:
Pridružite se seriji susreta kako biste s kolegama programerima i stručnjacima izgradili skalabilna rješenja umjetne inteligencije temeljena na stvarnim slučajevima upotrebe.