This article is a mirror article of machine translation, please click here to jump to the original article.

View: 31087|Reply: 0

[Source] C# Anonymous Type Learning Diary

[Copy link]
Posted on 10/16/2020 5:41:04 PM | | | |
When we don't want to define complex classes like complex methods, events, and constructors, we can dynamically generate a custom data type -> anonymous type.

1. Define the type of anonymity

When defining an anonymous type, you need to use the var keyword and object initialization syntax.

var: The compiler automatically generates a new class definition at compile time (we can't see the name of the class in the C# code).

Initialization: It will tell the compiler to create private background fields and (read-only) properties for the newly created type.

Construct an anonymous type by passing parameters and print the relevant information



Call: Anonymous types can also be built using hardcode



2. Internal representation of anonymous types

All anonymous types automatically inherit from Object, so we can use ToString, GetHashCode, Equals on the car object, let's try calling this:



Calls and results:







The type of the car object is: <>f__AnonymousType03 (yours may be different), the anonymous type name is felt by the compiler, we can't interfere, CIL code.

3. Implementation of methods ToString() and GetHashCode().

1.ToString()



2.GetHashCode()

It uses the variable of each anonymous type to calculate the hash value as the type input of System.Collections.Generic.EqualityComparer, and only <T>produces the same hash value if the two anonymous types have the same properties and are given the same value.

4. Equal semantics of anonymous types

Equals()





Analyze the results:

1. Equals(): The compiler overrides Equals() to determine that objects are equal based on value semantics (e.g., notes the value of each data member of two objects)

2. == Operators: Because anonymous types do not overload equivalent operators (== , !=), == compares references, not content.

3. GetType(): Because if we declare two identical (with the same attributes) anonymous types in the same assembly, the compiler will only generate a definition of one anonymous type.

5. Anonymous types that contain anonymous types



Summary:

In fact, we should be cautious about using anonymous types, especially when using LINQ, and never give up using strongly typed classes or structures because of the emergence of anonymous types.

In fact, the anonymous type itself has many limitations:

  • You don't control the name of the anonymous type
  • Anonymous types inherit System.Object
  • Anonymous types of fields and attributes are always read-only
  • Anonymous types don't support events, custom methods, custom operators, and custom rewrites
  • Anonymous types are implicit sealed
  • Entity creation of anonymous types uses only the default constructor


If we need to quickly define the shape of an entity without defining its function, we can use the anonymous type.




Previous:SQL Server data is exported as an insert script
Next:.net/C# reflects non-anonymous functions &lt;&gt;f__AnonymousType0 methods
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com