Nullable type tag (?)
illustrate:
- Nullable type tag
?
Used to indicate a value type (e.g.int
、float
etc.) can benull
. This is a feature introduced by C# 2.0, which is used to handle null values that may occur in scenarios such as database queries and JSON parsing.
Sample code:
int? nullableInt = 5; int? nullableIntWithNull = null; if () { (); // Output: 5} if (!) { ("The value is null."); // Output: The value is null.}
Null value propagation operator (?.)
illustrate:
- null value propagation operator
?.
Used in access may benull
The members of the object are checked before empty. If the object isnull
, it will not try to access its members, but will return directlynull
。
Sample code:
string str = null; int? length = str?.Length; // length is null, because str is null Person person = null; string jobTitle = person?.JobTitle; // jobTitle is null, because person is null person = new Person { JobTitle = "Software Engineer" }; jobTitle = person?.JobTitle; // jobTitle is "Software Engineer"
Conditional (ternary) operator (?:)
illustrate:
- Conditional operator
?:
is a ternary operator in C#, used to select one of two values based on the condition. The syntax iscondition ? expression1 : expression2
。
Sample code:
int x = 10; int y = 5; int max = x > y ? x : y; // max is 10 ("Maximum: " + max);
Null value merge operator (??)
illustrate:
- Null value merge operator
??
Used in the first operand isnull
Returns the value of the second operand, otherwise returns the value of the first operand. This is often used to provide default values to avoidnull
The exception raised.
Sample code:
string name = null; string displayName = name ?? "Anonymous User"; // displayName is "anonymous user" string username = "DeveloperDave"; displayName = username ?? "Newbie"; // displayName is "DeveloperDave", because username is not null (displayName);
Null value merge assignment operator (??=)
illustrate:
- Null value merge assignment operator
??=
is a feature introduced in C# 8.0 and later, which combines the null value merge operator (??
) and assignment operators (=
) function. This operator is used to assign the value on the right to the variable on the left, but only if the variable on the left isnull
hour. If the variable on the left is notnull
, its value will not be changed. This provides a more concise way to make possiblenull
variables provide default values or update their values without writing additionalif
Statement.
Sample code:
string firstName = null; firstName ??= "unknown"; // If firstName is null, set it to "Unknown"(firstName); // Output: Unknown firstName = "John"; firstName ??= "unknown"; // firstName is not null, so its value will not be changed(firstName); // Output: John // Example uses nullable typeint? age = null; age ??= 30; // If age is null, set it to 30( ? () : "null"); // Output: 30 age = 25; age ??= 30; // age is not null, so its value will not be changed( ? () : "null"); // Output: 25 // Example uses properties of class objectsPerson person = null; person ??= new Person { Name = "Default Name" }; // If person is null, create a new instance and assign a valueif (person != null) { (); // If person is a newly created instance, output: Default name} person = new Person { Name = "John Doe" }; ??= "Default Name"; // Not null, so its value will not be changed(); // Output: John Doe
In the example above,??=
The operator first checks whether the variable (or attribute) on the left isnull
. If yes, set it to the value on the right; if not, leave its current value unchanged. This operation may be asnull
variables or update them under certain conditions.
Please note that in the last example, try using it??=
Come updatePerson
The object'sName
Properties may not work as expected because??=
It is designed specifically for variable assignments, not for attributes. Use on attributes??=
This will result in a compilation error unless the property is special (such as an automatic implementation property of a nullable value type).
This is the end of this article about the use of C#?. For more related content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!