In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of checking null grammar sugar in C#, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this C# check null grammar sugar article. Let's take a look.
Traditional writing method of function parameter null check
When writing a function, the most classic check is probably the most commonly used null check, which should go like this:
Public static void GetV1 (string prompt) {if (prompt is null) throw new ArgumentNullException (nameof (prompt)); Console.WriteLine (prompt);} ThrowIfNull
There's nothing wrong with this, but I always feel a little uncomfortable. .net 6 adds the ThrowIfNull method to ArgumentNullException so that it can be written more elegantly.
Public static void GetV2 (string prompt) {ArgumentNullException.ThrowIfNull (prompt); Console.WriteLine (prompt);}
When an exception occurs, it appears: System.ArgumentNullException: 'Value cannot be null. (Parameter' prompt')'. Isn't this a little simple? But you still need to write a line.
C# 11! Syntax (cancelled)
I spotted this feature in C# 11 when I first started preview, and it can still be enabled by setting preview, but it's not likely to be officially released later.
It can be superimposed after the parameters! Indicates that this parameter cannot be empty and will be checked automatically by the runtime. If it is null, the error will pop up directly.
Public static void GetV3 (string roomy!) {Console.WriteLine (prompt);}
This code will be translated by the compiler into:
Public static void GetV3 (string prompt!) {if (prompt is null) {throw new ArgumentNullException (nameof (prompt));} Console.WriteLine (prompt);}
In this way, you can focus on the business code and don't have to think about exception checking too often. As for why this thing was finally deleted, we can see a hint from the discussion, first of all, it feels very entangled with this grammar, two exclamation marks, and then there are already many ways to check whether this thing is necessary. In the end, it will be discussed later, but it can be seen that the C # language working group is very cautious about the features of the language.
They also discussed many other forms, each of which put forward its own advantages and disadvantages. It is interesting to see that there is a little bit of rigor and obsessive-compulsive disorder in the design language. Like.
Void M (string slots!); 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 on null
Speaking of which, by the way, there are a few other grammatical sugars that c # handles null.
??
If the null is on the left, return the Operand on the right, otherwise return the Operand on the left, which is very useful in assigning default values to variables.
Int? A = null;int b = a??-1 * * Console.WriteLine (b); / / output:-1 benchmark =
When null is on the left, assign the variable on the left to the one on the right
Int? A = null;a?? =-1 * * Console.WriteLine (a); / / output:-1cm.
When the left side is null, then do not perform the following operation, directly return empty, otherwise return the actual operation value.
Using System;public class C {public static void Main () {string I = null; int? Length = length. Length; Console.WriteLine (length?-1); / / output:-1}? []
Indexer operation, similar to the above operation
Using System;public class C {public static void Main () {string [] I = null; string result = I? [1]; Console.WriteLine (result?? "null"); / / output:null}}
Note that if in the process of chaining, 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 actions will have different results.
Using System;public class C {public static void Main () {string [] I = null; Console.WriteLine (I? [1]? .Substring (0) .length); / / No error Console.WriteLine ((I? [1]? .Substring (0)) .length) / / System.NullReferenceException: Object reference not set to an instance of an object. }} some operation / / parameters give the default value if (x = = null) x = "str"; / / replace x? = "str"; / / conditional judgment string x if (I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.