in a flashC# 9
It's been 4 years since the release, and forrecord
Keywords must be no stranger to you, but it is found that there are still many students do not bother to use thissyntactic sugar
Indeed, in essence, record is the class of encapsulation, can be written in record class, that 100% can be hand-jerked out, but have you considered that other people may be a minute to write good code you may need a few minutes to complete. So in order to have more time to fish, we highly recommend those who don't care to use it!
I'll talk briefly about the benefits of record and the best scenarios.
- simplified syntax
We only need one line of code can be defined , this is the most intuitive way to save coding , we do not need to write a bunch of boring get; set , and do not need to write constructors and other sample code: .
public record Person(string FirstName, string LastName);
Then there will be students will have questions, if Person has a lot of attributes how to organize, does not mean that the main constructor will be very long, in fact, this is the same way and encapsulation of the passing of parameters, we can encapsulate the homogeneous attributes into other record or class, such as: the main constructor will be very long, in fact, this and encapsulation of the passing of parameters, we can encapsulate the homogeneous attributes into other record or class, such as.
public record ExtraInfomation(string Address,string Email,int Age);
public record Person(string FirstName, string LastName, ExtraInfomation ExtraInfo);
- Automatically generates some member functions that are useful to us.
- Constructor: automatically generates a constructor based on the defined properties.
- Attributes: Automatically generate read-only attributes.
- Deconstruct method: used to deconstruct the record object, for the habit of writing the
TS
The little ones are quite friendly. - Equals and GetHashCode methods: Equality comparison based on property values.
- ToString method: provide friendly string representation, especially friendly for debugging output.
- Value-based equivalence syntax .
We often have the need to compare all the attributes of a class to determine the logic. If we use record, we only need==
orEquals
I can tell.
- Non-destructive replication values
For a shallow copy of a class, we may need to implement theICloneable
Or, you could just create a new object and assign values to each of its attributes. There are other ways to do this, but they're certainly not as straightforward as record. All we need is awith
The keyword is done.
public record Person(string FirstName, string LastName, int Age);
var person1 = new Person("vip", "wan", 18);
var person2 = person1 with { Age = 31 };
(person1); // exports: Person { FirstName = vip, LastName = wan, Age = 30 }
(person2); // exports: Person { FirstName = vip, LastName = wan, Age = 31 }
- Deconstructed support
The record type automatically generates the Deconstruct method, allowing you to easily deconstruct the record object, for full-stack students writing is handy!
var person = new Person("vip", "wan", 18);
var (firstName, lastName, age) = person;
(firstName); // exports: vip
(lastName); // exports: wan
(age); // exports: 18
- Filling of existing classes
Well, the current C# language is really coming along, and will be released by the end of the year.C# 13
All right, guys.They say they can't learn anymore.! - (LAUGHS) I'm sure some of my classmates have tried it too.principal constructor (math.)
If you want to know more about the main constructor you can use theclick on a link For injected services there is again a lot less code to jerk around!
So now that the classes are availableprincipal constructor (math.)
Doesn't that mean the record is meaningless? Huh? Did you forget the sweetness of the sugar up there?
So if we want to support the record feature for an existing class we can just prefix the class with record.
public record class User {
public string UserName{ get; set;}
public int Age { get; set;}
}
var user1 = new User { UserName = "vipwan" , Age = 18};
var user2 = user1 with { };
var user3 = user1 with { Age = 30 };
user1 == user2 // true;
() // "User { Name = vipwan, Age = 30 }"
summarize
The main benefits of using record types include concise syntax, auto-generated members, value-based equivalence, non-destructive copying, deconstruction support, inheritance support, and the ability to work with thepattern matchingThese features make the record type ideal for use with immutable data objects (DTOs, VOs, etc.). These features make the record type ideal for immutable data objects (DTOs, VOs, etc.), improving the code'sreadable、maintainabilitycap (a poem)development capability。