At the time of last year's PDC2005, when c#2.0 (C # Whidbey) was released, Microsoft also showed some of their plans on C # 3.0. Referring to a range of new language features such as language-Integrated query (LINQ), Redmond also introduces a new feature-anonymous type. This article describes the anonymous type in detail.
Anonymous type definition
The C#3.0 specification describes an anonymous type as a tuple type that is automatically inferred and generated from the object initializer (objects initializer). Before you can fully comprehend this definition, you need to understand the concept of the object initializer, which is the basis for anonymous type attributes.
The object initializer assigns a value to one or more fields or properties of an object. This means that you can specify a series of attributes for an object through an assignment operation such as {a=10,b=20}. In other words, an anonymous type is not present and is not explicitly specified in the code.
Note that the compiler creates anonymous types at compile time rather than runtime.
You can decompose by ildasm (il factorization) to obtain:
var P1 = new {Name = ' A ', price = 3};
At compile time, the compiler uses the property inferred by the object initializer to pass on a new anonymous type. Thus, the new type will have the properties of name and price. The Get and set methods and the corresponding private variables that hold these properties are automatically generated. At run time, an instance of this type is created, and the property of this instance is set to the value specified in the object initializer.
C # Internal
You may be surprised to find that you can just define the names of some properties and their values, and C # 3.0 will automatically create classes from them. How did this happen? Check the compiler's handling.
This starts a line of code:
var P1 = new {Name = ' A ', price = 3};
When the C # 3.0 compiler encounters such a request, it translates it into a more explicit representation in the background, as follows:
Class __anonymous1
{
private string name;
private int price;
public string name{get {return Name;} set {name = value;}}
public int price{get {return price;} set {price= value;}}
}
__anonymous1 P1 = new __anonymous1 ();
P1. Name = "A";
Pt. Price =3
Example Learning
You need to install Visual Studio 2005 and. NET 2.0, and then you can download the preview version of LINQ technology from here.
If you installed Visual Studio 2005, you can see that there are 3 more engineering templates related to LINQ previews in Visual C #: LINQ Console Application, LINQ Windows Application, and LINQ Library.
You can create a project that uses an anonymous type like this:
1. Open the Visual Studio 2005 editor, create a new project, select LINQ Console as the engineering template;
2. Name the new project anontypes and click OK;
3. Enter the following code in the editor:
Program.cs
Using System;
Using System.query;
Using System.Data.DLinq;
Namespace Anontypes
{
Class Program
{
static void Main (string[] args)
{
var P1 = new {Name = ' A ', price = 3};
Console.WriteLine ("Name = {0}\nprice = {1}", p1.) Name, p1. Price);
Console.ReadLine ();
}
}
}
4. Compile the program
5. Implementation procedures, with the following results:
Name = A
Price = 3
If you don't have Visual Studio 2005, you can still compile your code from the command line:
C:\Program files\linq Preview\bin\csc.exe
/reference: "C:\Program files\linq Preview\bin\system.data.dlinq.dll"
/reference:system.dll
/reference: "C:\Program files\linq Preview\bin\system.query.dll"
/out:anontypes.exe/target:exe Program.cs
Although you don't have a clear definition of a class in your code, the C # compiler does the following things automatically:
1. Resolution Type
2. Create a new class (owning the name and price attributes)
3. Use this class to initialize a new object
4. Assign the parameters to the object
Deep Parsing code
To see how the compiler creates a new class, open ildasm (under C:\Program Files\Microsoft Visual Studio 8\sdk\v2.0\bin) and select the most recent compilation assembly, AnonTypes.exe. With the tree view open, you can see the view as shown in Figure 1:
Figure 1
If you look closely, ILDASM shows how an anonymous type "<projection>f__0" is created. and classes are created at the same time by private variables _name and _price. Both the get and set methods for these two variables are also created, and they also have property name and price.
Double-click on any method or variable to see clearly, as you click on the Name property, you will see the following code:
. property instance String Name ()
{
. Get instance String anontypes.program/
' <projection>f__0 ':: Get_name ()
. Set instance void anontypes.program/
' <projection>f__0 ':: Set_name (String)
}//End of ' <projection>f__0 ':: Name
Multiple anonymous Types
If you create a number of similar anonymous types, the C # compiler will be smart enough to find this, generating only a class and its two instances, such as you enter the following code:
Using System;
Using System.query;
Using System.Data.DLinq;
Namespace Anontypes
{
Class Program
{
static void Main (string[] args)
{
var P1 = new {Name = ' A ', price = 3};
var P2 = new {Name = ' A ', price = 3};
Console.WriteLine ("Name = {0}\nprice = {1}", p1.) Name, p1. Price);
Console.ReadLine ();
}
}
}
When you compile and open with ILDASM, the structure looks like this:
Figure 2
As you can see, because the declarations are similar, C # creates only one anonymous class, thus optimizing the program. But if they are not very similar, there will be two anonymous classes.
Modify the above code slightly, as follows:
New {Name = ' A ', price = 3};
var P2 = new {Name = ' A '};
Figure 3
As you can see, C # creates two classes, <projection>f__0 and <projection>f__1.
Summary
As we can see here, anonymous types provide a mechanism for programmers, which makes it unnecessary for you to declare class structures clearly. Also, the C # 3.0 compiler is very intelligent, creating only one anonymous type when you have multiple similar anonymous types to achieve the optimizer's purpose.