Today I saw that the devblogs has been updated, the new C# 11!! (for checking null syntax) has been very longdiscuss,at lastCanceled. Then I remembered null check again, and I can say this.
Function parameter null check
Traditional writing
When writing a function, the most classic check is probably also the most commonly used null check. It should be like this:
public static void GetV1(string prompt) { if (prompt is null) throw new ArgumentNullException(nameof(prompt)); (prompt); }
ThrowIfNull
This is not a problem, but I always feel a little uncomfortable. .NET 6 is inArgumentNullException
Newly added toThrowIfNull
Methods can be written more elegantly.
public static void GetV2(string prompt) { (prompt); (prompt); }
When an exception occurs, it will appear:: 'Value cannot be null. (Parameter 'prompt')'
. Is this a little simpler? But you still need to write a line.
C# 11!! Syntax (canceled)
When C# 11 was just previewed, I noticed this feature. Now I can still enable it by setting the preview, but it will probably not work if it is officially released in the future.
It superimposes after the parameter!! means that this parameter cannot be empty, and it will automatically check when running. If it is null, an error will pop up directly.
public static void GetV3(string prompt!!) { (prompt); }
This code will be translated by the compiler:
public static void GetV3(string prompt!!) { if (prompt is null) { throw new ArgumentNullException(nameof(prompt)); } (prompt); }
In this way, everyone can focus on business code and do not need to consider exception checks frequently. As for why this thing was deleted in the end, you can see a hint of clue from the discussion. First, I feel very entangled in this grammar and two exclamation marks; then there are already many ways to check whether this thing is necessary. Anyway, it will be discussed later, but it can also be seen that the C# language working group is very cautious about the discussion of language characteristics.
They also discussed many other forms, each of which proposed their ownPros and consIt's quite interesting, you can see that there is a little rigorous design language and little obsessive-compulsive disorder. Like~
void M(string s!!); void M(string! s); void M(string s!); void M(notnull string s); void M(string s ?? throw); void M(string s is not null); void M(checked string s); void M(string s) where s is not null;
Some operations about null
Speaking of this, let’s talk about some other syntax sugars for c# to handle null.
??
If the left is null, then the operand on the right is returned, otherwise the operand on the left is returned. This is very useful when assigning the default value to the variable.
int? a = null; int b = a ?? -1; (b); // output: -1
??=
When the left is null, then the left variable is assigned to the right
int? a = null; a ??= -1; (a); // output: -1
?.
When the left is null, then the following operation is not executed and it will return empty directly, otherwise the actual operation value will be returned.
using System; public class C { public static void Main() { string i = null; int? length = i?.Length; (length ?? -1); //output: -1 } }
?[]
The indexer operation is similar to the above operation
using System; public class C { public static void Main() { string[] i = null; string result = i?[1]; (result ?? "null"); // output:null } }
Note that if during the chaining process, as long as one of the previous operations is null, the null result will be returned directly and the calculation will not continue. The following two operations will have different results.
using System; public class C { public static void Main() { string[] i = null; (i?[1]?.Substring(0).Length); //No error ((i?[1]?.Substring(0)).Length) // : Object reference not set to an instance of an object. } }
Some operations
//The parameters are given to the default valueif(x == null) x = "str"; //replacex ??= "str"; // Conditional judgmentstring x; if(i<3) x = y; else { if(z != null) x = z; else z = "notnull"; } //replacevar x = i < 3 ? y : z ?? "notnull" //Prevent the code from being executed when the object is nullif(obj != null) (); //replaceobj?.Act(); //Dictionary value and assignmentstring result; if((key)) { if(dict[key] == null) result = "There is a result of null"; else result = dict[key]; } else result = "No result is null"; //replacevar result= (key, out var value) ? value ?? "There is a result of null" : "No result is null";
Conclusion
It turns out that the newly-designed C# 11 provides a new one. By the way, I personally like this feature very much. No matter what form it appears, I look forward to seeing you in the future.
In C#, we have prepared a lot of syntax sugar for us in order to deal with null, which can only be said to be very simple. Many people may say that this is not readable, or think that this is a crooked way like "several ways of writing the word "fen". I can only say that traditional grammar does not mean that it has been cancelled. The language has developed. As long as it is still relatively cautious, I think it is still a good thing.
This is the end of this article about detailed explanation of the syntax sugar for checking null in C#. For more related content on checking null in C#, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!