1. Nullable type modifier?
// The reference type can be used to represent a non-existent value, but the value type cannot. For example:string str = null; int i = null;//Compilation error // In order to make the value type also use nullable types,// You can use "?" to represent it, and the expression is "T?". For example:int i?; //Denotes a nullable typeDataTime time?; //Indicates the time that can be vacancied
2. Empty merge operator??
// Default values for defining reference types and nullable types.// If the left operator of this operator is not Null, this operator returns the left operand,// Otherwise, return the right operand.// Return a when a is not empty, return b when it is nullvar c = a ?? b;
3. Continue to execute subsequent code when non-null, operator?.
// Perform the following operations when not null. For example: // The following two pieces of code are equivalent, that is, the subsequent logic will continue to be executed when it is not null?.; = Person == null ? null : ;
4. ??=
// C# 8.0 and later can use the null merge assignment operator ??=,// This operator is only when the evaluation result of the left operand is null,// Assign the value of the operand to the right to the operand to the left.// If the calculation result of the left operand is non-null,// Then the ??= operator will not calculate its right operand. List<int> numbers = null; int? i = null; numbers??= new List<int>(); (i ??= 17); (i ??= 20); (("", numbers));//output:17 17 (i);//output 17
This is the end of this article about the usage of ?, ?., ??, ??= operators in C#. For more related C#, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!