Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of anonymous types in C#

Tags:

c#

.net

Is it bad to use anonymous types in C#?

like image 315
ryudice Avatar asked Jun 23 '10 23:06

ryudice


People also ask

Why anonymous types are better than tuples?

Tradeoffs. You might want to always use ValueTuple over Tuple, and anonymous types, but there are tradeoffs you should consider. The ValueTuple types are mutable, whereas Tuple are read-only. Anonymous types can be used in expression trees, while tuples cannot.

What is an anonymous type in C?

Updated on: May 2, 2020. In C#, an anonymous type is a type (class) without any name that can contain public read-only properties only. It cannot contain other members, such as fields, methods, events, etc. You create an anonymous type using the new operator with an object initializer syntax.

What is the difference between an anonymous type and a regular data type?

The compiler gives them a name although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type, except that it cannot be cast to any type except for object.

Why do we need anonymous types in C#?

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.


1 Answers

No, it is not. They are code generated classes at compiletime and perform as well as normal classes.

like image 78
Femaref Avatar answered Oct 20 '22 00:10

Femaref