C#3.0 series: Anonymous Type in CLR (3.5)

Source: Internet
Author: User
Tags anonymous

We say that the anonymous type is a new feature of C # 3.0, not that anonymous type is a new feature of the. NET Framework 3.5. This is because the anonymous type is simply a newly introduced feature of the. NET programming language and the corresponding compiler. For the. NET Framework 3.5来, it does not see how this differs from the original, and there is no essential difference between the anonymous type and the general named type for the CLR.

Let's use a simple code like this:

1var p1 = new { Name = "IORI", Age = 27 };

And then we'll look at IL:

Code

1.method private hidebysig static void Main(string[] args) cil managed
2{
3 .entrypoint
4 // Code size    15 (0xf)
5 .maxstack 3
6 .locals init ([0] class '<>f__AnonymousType0`2'<string,int32> p1)
7 IL_0000: nop
8 IL_0001: ldstr   "IORI"
9 IL_0006: ldc.i4.s  27
10 IL_0008: newobj   instance void class '<>f__AnonymousType0`2'<string,int32>::.ctor(!0,
11                                             !1)
12 IL_000d: stloc.0
13 IL_000e: ret
14} // end of method Program::Main
15

We can see here again that compiler will create a type named <>F__ANONYMOUSTYPE0 ' 2<string,int32> for P1 this anonymous type. The arguments will be based on the specific structure of the data member, which is also declaring a type. This class inherits from object and overrides the implementation of ToString () and GetHashCode (). All anonymous types are automatically inherited from System.Object, and Equals (), GetHashCode (), and ToString () are overridden. The following figure:

The structure is as follows:

internal sealed class <>f__AnonymousType0<<Name>j__TPar, <Age>j__TPar>
{
  // Fields
private readonly <Age>j__TPar <Age>i__Field;
    private readonly <Name>j__TPar <Name>i__Field;

   // Methods
public <>f__AnonymousType0 (<Name>j__TPar Name, <Age>j__TPar Age);

   public override bool Equals(object value);
    public override int GetHashCode();
    public override string ToString();

  // Properties
  public <Age>j__TPar Age { get; }
  public <Name>j__TPar Name { get; }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.