C # What does 3.0 bring to us (5) -- anonymous type

Source: Internet
Author: User

VaR Employee = new {age = 25, name = "James "}
You do not need to declare a class, and you can get the context variable -- closure in the initialization tool.
This is the anonymous type provided in C #3.0.
In addition, the declared type can be accessed as follows:
String name = employee. Name;

Let's take a look at the implementation of Il.
. Method public hidebysig instance void dd () cel managed
{
// Code Size 22 (0x16)
. Maxstack 3
. Locals Init ([0] class' <> F _ anonymoustype0 '2' <int32, string> employee,
[1] string name)
Il_0000: NOP
Il_0001: LDC. i4.s 25
Il_0003: ldstr "James"
Il_0008: newobj instance void class '<> F _ anonymoustype0 '2' <int32, string >::. ctor (! 0,
! 1)
Il_000d: stloc.0
Il_000e: ldloc.0
Il_000f: callvirt instance! 1 class '<> F _ anonymoustype0 '2' <int32, string >:: get_name ()
Il_0014: stloc.1
Il_0015: Ret
} // End of method testvar: dd
When you see the code, you will think, '<> F _ anonymoustype0 '2', where does it come from?
Let's see the Il view.

Although the anonymous type is very convenient, we cannot use the code to access '<> F _ anonymoustype0 '2', and VAR cannot define global variables and parameter types, therefore, the created anonymous instance can only be applied within the function. This restricts the use of the anonymous type.

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.