In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use the operator for NULL in C #, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.
? . And? []
The null conditional operator is available after null 6, and accesses members or elements only if the Operand is non-cantilever.? . And? [] it's easy to tell; we know'.' Access to members or life space,'[] 'indexer access, the following demonstrates the use of operators:
Static double? SumNumbers (List setsOfNumbers, int indexOfSetToSum)
{
/ / if the setsOfNumbers is not empty, access the specified index; if the index of the corresponding element is not empty, the sum
Return setsOfNumbers? [indexOfSetToSum] .Sum ()
}
Var sum1 = SumNumbers (null, 0)
Console.WriteLine (sum1??Double.NaN); / / output: NaN
Var numberSets = new List {new [] {1.0,2.0,3.0}, null}
Var sum2 = SumNumbers (numberSets, 0)
Console.WriteLine (sum2?? Double.NaN); / / output: 6
Var sum3 = SumNumbers (numberSets, 1)
Console.WriteLine (sum3?? Double.NaN); / / output: NaN
??
What do you mean by the NUll merge operator? That is, if this value is empty, another value is used, called null b. If an is not empty, the result is a; otherwise, the result is b. The operation calculates b only if an is null. Common scenarios such as: using throw expressions as?? The right Operand of the operator, detect data, assign default values when the get is empty, and so on.
Var comment = _ blogService.GetBlogCommentById (id)
?? Throw new ArgumentException ("the specified id is to find the corresponding data!" , nameof (id))
?? =
Operator? = is the null merge assignment operator introduced in Cnotify 8.0. What does it mean? The operator can only be used when the left Operand evaluates to null. = assign its value with an operator to the left Operand. The example code is as follows:
List numbers = null
Int? I = null
Numbers? = new List ()
Numbers.Add (I = 66)
Numbers.Add (I? = 99)
/ / equivalent to the following code
/ / if (i==null)
/ / {
/ / I = 66
/ / numbers.Add (i.Value)
/ /}
/ / if (I = = null)
/ / {
/ / I = 99
/ /}
/ / numbers.Add (i.Value)
Console.WriteLine (string.Join ("", numbers)); / / output: 66 66
Console.WriteLine (I); / / output: 66 Thank you for reading this article carefully. I hope the article "how to use the operator of NULL in C#" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.