Hello.
In this lecture, we're going to talk about Question 31, what our records and records structs when
programming, we often need to define simple data structures that don't really hold any business logic.
They simply stole data.
Let's define a point class.
This class only holds two integers X and Y.
I want the object of this class to be immutable.
So one cert they will not be updated.
That's why I only added letters to the X and Y properties.
Setters are not available.
First of all, I would like the objects of this class to be nicely printed.
Let's see how this object will be printed now.
Well, currently the full name of the type printed, but I would rather see something like X is done
and Y is twenty.
That's why we should overwrite the two string method.
Perfect.
That looks much better.
Next, I would like to use objects of the pond grass, a squeeze in a dictionary currently, since point
is across the guitar called method basis on reference and so does the equals method, I should overwrite
those methods if I wanted the dictionary to recognize objects of the point grass by value, not by reference.
No, I must also provide that equals method that accepts a point class object, not an instance of system
object type.
This means my point class shall implement the équitable of both class interface.
All right.
Let's see the whole class.
Well, it will work, but it's a lot of code and all of it only to implement a simple data structure
that brings itself nicely is compared by value, provides custom guitar code and equals method implementations
so it can safely be used in collections at some point.
The creators of C-sharp realized that this is a common issue as a solution.
They introduced records before I explain exactly what records are.
Let me show you how exactly the same behavior as we defined in the point class type can be achieved
with records.
And that's it.
Only one line of code.
And it does exactly the same thing as 31 lines of code.
We need that to define the point.
Class records are new types, joining classes and structs.
They are available starting with the shop.
Nine.
Let's list the most important information about records.
Records are reference types, but they based on value type equality, which means two records with identical
values of properties will be considered equal even if they differ by reference like classes.
They support inheritance.
The compiler generates the following methods for their records and overwrite of the equals method accepting
an object a virtual equals method accepting another instance of this record type.
This method comes from the équitable interface, which records implement an override for the guitar.
Automated overloads loads of equality operators.
An override of the two string method, which brings the names of the properties with their values.
The record we defined here is even more special.
It's so-called positional record, so a record that doesn't even have a body.
Later in this video, we learn how to define non positional records.
For now, let's just note that for positional records, the compiler also generates a primary constructor
whose parameters match the positional parameters of the record declaration public properties for each
parameter of this constructor.
Those properties are read only, but they are not for record struct which will learn about in a minute.
Also, I deconstruct method, which allows using deconstruction.
All right, now, let's see a regular positional record.
As you can see, we need to write a little more code, like explicitly defining the properties and the
constructor, but as a reward, we can add methods or make the properties readable.
Remember that methods like hash code equals or to string are still generated by the compiler, and we
don't need to worry about them, as we learned in the last lecture.
The immutability of types is a desired trait.
Records are perfect for representing immutable types to make things even easier.
They provide non-destructive annotation implemented with the wish keyword.
This may sound cryptic, so let's see an example.
Let's say I have some bond and I want to update its Y property, but in a non-destructive way.
With the work you worked, I created a new point equal to the old one, but with one set to the new
value, the old bond is immutable, so it cannot be changed.
I turn as many properties as I want.
There were few words starting with C-sharp, then records structs were introduced.
They are similar to records.
Well, some differences.
First of all, they are value tags, positional records.
Structs are read right by default, which means their properties are immutable.
Record structs can be declared as read-only, making them immutable.
So I want to be able to modify the instances of this struct because it is with only but for disrupts,
the properties are immutable when deciding whether to use records or reconstruct.
We should take the same things into consideration as when deciding whether to use classis or structs.
If the type is small and simple and you want value Typekit behavior like putting parameters by value
or assignment that creates a copy.
You should go for the record struct records and record struct support the.
We learned about it in the What is the construction lecture?
Let's summarize records and records tracts are new types introduced in Europe nine and 10.
They are mostly used to define simple types representing data.
They support value based equality.
They make it very easy to create immutable types.
Questions about records.
Are recently quite common during the interviews because the interviewers often wanted to check if you
are up to date with the new versions of C-sharp.
So, for example, you can hear a question What is the purpose of the whiskey worth?
The whiskey world is used to create a copy of our record object, with some properties set to new values.
In other words, is used to perform a non-destructive imitation of records.
What are positional records?
Positional records?
Our records with nobodys.
The compiler generates properties constructor and deconstruct method for them.
There are a shorter way of defining records, but we can't add custom methods or about properties to
our position on record.
All right, that's it.
In this lecture, I hope you'll find it useful and that you start using records in your code.
Thanks for watching and see you in the next video.