In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you the ten-mile discovery of record types, pattern matching, and init attributes in C#. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
I. record type
Record, I'd better use the original word. I know it has been translated as "record type". It's just, Lao Zhou always thinks it doesn't sound good, but Lao Zhou can't find a better word, so let's go back to record.
Record is a reference type, much like class. So, is it not fragrant to use the class that the people are familiar with? why add a new record? A: for the convenience of data comparison.
I don't understand? It's all right. Look down. Recently, a warm-hearted neighbor gave Lao Zhou a pet:
Public class Cat {public string Nick {get; set;} public string Name {get; set;} public int Age {get; set;}}
This new pet is not easy, a top-of-the-range foodie. Fish, pork, drumsticks, biscuits, tofu, bread, fruit, noodles, wheat, moths. Anyway, as long as it can be stuffed into its mouth, it will eat.
Next, let's new two pet examples.
/ / two instances describe the same cat Cat pet1 = new Cat {Nick = "pine nut", Name = "Jack", Age = 1} Cat pet2 = new Cat {Nick = "pine nut", Name = "Jack", Age = 1}; / / it is not the same cat Console.WriteLine ("the same cat? {0} ", pet1 = = pet2)
In fact, both examples describe my family's darling. However, the output is:
The same one? False
This is because, in the equality comparison, people are concerned about whether the type reference is the same instance. However, in the data processing scenario, we are more concerned about whether the fields / attributes in the object are equal, that is, content comparison.
Now, change the declaration of Cat to type record.
Public record Cat {public string Nick {get; set;} public string Name {get; set;} public int Age {get; set;}}
Then make an equal comparison with the above pet1 and pet2 instances, and get the expected results:
The same one? True
The record type saves you the logic of overriding equality comparisons (overriding methods such as Equals, GetHashCode, or overloading operators).
In fact, the compiled record type of the code is also a class, but automatically implements the logic of member equality comparison. What you used to do manually is now left to the compiler.
Suppose you have a User type that represents user information (including user name, password), and then this User type may produce multiple instances in the data processing scenario. For example, you filter out a User instance A from the EF model according to the criteria, and generate User instance B based on the login and password entered by the user. To verify that the login information entered by the user is correct, if User is class, you may want to judge as follows:
If (A.UserName = = B.UserName & & A.Password = = B.Password) {.}
But if you define User as a record type, then, in one sentence:
A = B
Pattern matching (Pattern Matching)
The translation of "pattern matching" feels strange, and Lao Zhou hasn't come up with any better words yet. Pattern matching is not a magic thing, it's just an extended behavior when detecting the value of a variable. In the past, I used to feel that the switch statement of C++/C# was not powerful enough, because in traditional usage, each case clause could only compare a single constant value. such as
Int score = 85; switch (test result) {case 10: Console.WriteLine ("only scored so little"); break; case 50: Console.WriteLine ("close enough to pass"); break Case 85: Console.WriteLine ("really show"); break; case 90: Console.WriteLine ("miracles"); break;}
I fantasized that it would be nice to write something like this:
Switch (examination result) {case 0: Console.WriteLine ("missed examination?") ; break; case > 0 & & 30 & &
< 60: Console.WriteLine("还是不行"); break; case >= 60 & &
< 80: Console.WriteLine("还得努力"); break; case >= 80 & &
< 90: Console.WriteLine("秀儿,真优秀"); break; case >= 90 & & 0 and 30 and
< 60: Console.WriteLine("还是不行"); break; case >= 60 and
< 80: Console.WriteLine("还得努力"); break; case >= 80 and
< 90: Console.WriteLine("秀儿,真优秀"); break; case >= 90 and 1000f}: Console.WriteLine ("rich, rich"); break; case {Qty: > 500f}: Console.WriteLine ("good boy, big order of the year"); break Case {Qty: > 100f}: Console.WriteLine ("good order quantity"); break;}
Huh? What, what the heck is this? Don't be surprised. It's not a ghost. It means to judge the value of the Qty attribute, if the order volume is greater than 100, the output "order quantity is good"; if the order volume is greater than 1000, then the output "rich, rich".
But you would say, how did this pair of curly braces come from? Do you remember this way of writing LINQ?
From x in... Where x.A... Select new {Prop1 =..., Prop2 =. }
New {...} is an anonymous type instance, so if it is a non-anonymous type, take a look at the previous Cat instance initialization.
Cat {. }
That's right, this pair of curly braces are used to construct the member values of an instance, so the above switch statement actually reads like this:
Switch (od) {case Order {Qty: > 1000F}: Console.WriteLine ("get rich, get rich"); break; case Order {Qty: > 500f}: Console.WriteLine ("good guy, big order of the year"); break Case Order {Qty: > 100f}: Console.WriteLine ("good order quantity"); break;}
Order {.} is to match an instance of an Order object, and its Qty property must match. Conditions. Because the variable od is always of type Order, the Order in the case clause is omitted and becomes
Case {Qty: > 1000F}: Console.WriteLine ("get rich, get rich"); break
If more than one attribute appears, it means that the matching condition is set for multiple attributes, and there is a "and" relationship between them. such as
Case {Qty: > 100F, Company: not null}: Console.WriteLine ("good order quantity"); break
Guess what that means? This can be literal, the value of the Qty attribute should be greater than 100, and the value of the Company attribute cannot be null. The way not to write for null is not null, don't write it as! null, because it's too ugly.
If you have fewer branches of code, you can use the if statement, just with the is operator.
If (od is {UP:
< 3000M }) { Console.WriteLine("报价不理想"); } 但是,这个写法目前有局限性,它只能用常量值来做判断,你要是这样写就会报错。 if (od is { Date: < DateTime.Now }) { ................ } DateTime.Now 不是常量值,上面代码无法通过编译。 is 运算符以前是用来匹配类型的,上述的用法是它的语法扩展。 object n = 5000000L; if(n is long) { Console.WriteLine("它是个长整型"); } 进化之后的 is 运算符也可以这样用: object n = 5000000L; if(n is long x) { Console.WriteLine("它是个长整型,存放的值是:{0}", x); } 如果你在 if 语句内要使用 n 的值,就可以顺便转为 long 类型并赋值给变量 x,这样就一步到位,不必再去写一句 long x = (long)n 。 如果 switch... 语句在判断之后需要返回一个值,还可以把它变成表达式来用。咱们把前面的 Order 例子改一下。 string message = od switch { { Qty: >1000F} = > "made a fortune", {Qty: > 500f} = > "Big order of the year", {Qty: > 100f} = > "order quantity is good", _ = > "unknown"}; Console.WriteLine (message)
At this point, you have to pay attention to:
Switch is now an expression, not a statement block, so the semicolon to the right of the curly braces cannot be reduced.
Because switch becomes an expression, you can't use the case clause, so match it directly with the specific content.
Finally, the underscore (_) of "unknown" is returned, that is, the so-called "abandoned baby", oh no, it is "abandoned element", that is, variables that are assigned values but do not need to be used can be discarded directly. This is equivalent to the default clause in the switch statement block, returning "unknown" when all the previous conditions do not match.
Third, the init accessor of the attribute
First of all, it is important to know that this init is only used for the initialization phase of read-only properties, and for read-and-write properties, as before, you can simply get; set;.
Some people say that this init does not know what to do, so let's not talk about it first, let's take a look at the attribute initialization statements added in previous versions of C #.
Public class Dog {public int No {get;} = 0; public string Name {get;} = "no name"; public int Age {get;} = 1;}
You see, this allows you to assign an initial value to an attribute, so why do you need init?
OK, I'll create a problem for you-- if I initialize the properties of the Dog class like this, give it a try.
Dog x = new Dog {No = 100, Name = "Gigi", Age = 4}
Give it a try. Compilation will go wrong.
In some cases, you can assign initial values during the property definition phase, but sometimes you have to initialize them in the code. In the past, we would solve this problem by defining constructors with parameters.
Public class Dog {public int No {get;} = 0; public string Name {get;} = "no name"; public int Age {get;} = 1; public Dog (int no, string name, int age) {No = no; Name = name; Age = age;}}
Then, initialize it like this.
Dog x = new (1001, "Gigi", 4)
However, the pretending index of doing so is still not high enough, you can't do this for every class. It's not too hard, but each class has to write a constructor, which is not neat.
So the init accessor came in handy, and we changed the Dog class.
Public class Dog {public int No {get; init;} public string Name {get; init;} public int Age {get; init;}}
Instead of writing constructors with parameters, you assign values to attributes directly when instantiated.
Dog x = new Dog {No = 100, Name = "Gigi", Age = 4}
As a result, these read-only properties have default initial values.
Of course, this assignment is only valid during initialization. After initialization, you want to change the value of the attribute. No way!
X.Name = "Dong Dong"; / / error x.Age = 10; / / Thank you for your reading! This is the end of this article on "finding ten miles of record types, pattern matching and init attributes in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.