Hello.
In this lecture will focus on Question 35, What are anonymous types?
Anonymous types are types without names.
Anonymous types provide a convenient way of encapsulating a set of only properties into a single object
without having to explicitly define the type first.
As you can see, to create an object of an anonymous type, we simply use the new keyword and then put
on the properties we want in the braces.
Here we created an anonymous type with free properties, name and city of type, string and age of type
int the properties of anonymous types are read only, so the code modifying them will not compile.
To understand better what may be the use case for anonymous types, let's consider a simple coding challenge.
First, let's define a collection of parts.
Each bird has a name type and wait.
What we want to do is to build a collection of strings that will contain data about each type, including
the average weight of pets of this type.
The result should be sorted by ascending.
In other words, it should look like this will use Link to do it.
If you don't know Link and you would like to learn it took Hold My Course Link Tutorial Master the key
C-sharp library in the last lecture of this course, you can find a discount coupon.
All right.
First, we need to group the pets by type.
For each of the groups, we want to calculate the average weight.
Well, this works fine, but there is one problem.
We only selected the average weights of each group now, but we lost the information about the name
of each of those groups.
We must change this cult to not select votes as the average weight is afloat, but pairs of prototype
cells.
But how should we represent those powers?
We could define a class struct or a record for it.
Again, this works, but it seems like a big effort.
We created a whole separate type for this very specific piece of data will probably never use it in
a different context, not to mention that this name is a bit awkward.
But how else could we call it?
There is really no good name for this very specific set of data.
The solution is to use an anonymous type.
Anonymous type is a type defined right where it's needed without even giving it a name.
It's perfect for use cases like ours when the type is small and temporary and we don't intend to use
it anywhere else.
All right.
So now this anonymous object will hold a prototype and a float representing the average weight of births
of this type.
The only thing left to do is to order this data and make it to some readable strings.
Let's make sure this works as expected.
All right.
Seems like our code using anonymous type works correctly.
Let's take another look at this type because it doesn't even have a name will not be able to use it
anywhere else because how could we refer to it if we don't know its name?
Actually, the compiler gives it a name that can be seen in the common intermediate language.
But even if we use it doesn't to find it, it won't be possible to use it just to satisfy your curiosity.
I joked how the compiler named this particular type.
He was the name of the anonymous type we defined.
As you can see, it's not very readable.
Please note that from the perspective of the common language runtime, anonymous types are no different
than other types.
It's least the most important information about anonymous types.
They contain only root only properties.
No other kinds of class members, such as methods or events, are valid.
If no names are given to the properties of the anonymous type, the compiler will use the name of the
property that was used to set the value of the anonymous Typekit property.
For example, if instead of this, we will do this.
The name of the first property would be key, the same as the name of the property assigned to it when
this assigned value is not a property or a field.
It must be given a name, for example.
In this case, it doesn't work because this value is calculated and it doesn't have a name.
We must give it a name.
Otherwise, this error will be seen.
Anonymous types are class objects derived directly from the system object type.
They can't be cast one of our type.
They override the equals and the cash court methods to support value based equality.
Two anonymous objects with the same values will have the same hash codes, and the equals method will
return to form them.
Please note that the equality operator is not overloaded, so it will return false because there are
reference types and they differ by reference.
Also, anonymous objects support non-destructive mutation.
Well, if there was keyword.
Remember, non-destructive mutation is not changing the original object, but rather creating a new
one.
We've turned to values.
Let's summarize Anonymous types are types without names.
They encapsulate a set of only properties into a single object without having to explicitly define a
type first.
They are most often use defined small, temporary types that we don't intend to use anywhere else.
During the interview, you can be asked Can we modify the value of an anonymous type of property?
No.
All properties of anonymous types are with only one.
Should we owned one, should we not use anonymous types?
The best use case for on animal stopes is one.
The type we want to use is simple and you'll come to some specific context and it will not be used anywhere
else.
Anonymous types are very often used as temporary objects in complex link queries.
If a type is complex or we want to use it, it should not be anonymous.
Also, anonymous types can only provide only properties.
They can't have methods, fields, events, etc. So if we need any of those features, the anonymous
types will not work for us.
Our anonymous types, value or reference types, they are reference types since they are crosses, but
they support value based equality with the equals method.
In other words, two anonymous objects with the same values of properties would be considered equal
by the equals method, even if the references are different.
All right, that's it about anonymous types.
Thanks for watching and see you in the next video.