The introduction of Linq in C #3.0 makes a profound change in the control of collections. The hero behind this change is the extension method and anonymous type. Here we will talk about anonymous and implicit type variables in C.
I,Anonymous type
The so-called anonymous type, as its name implies, isNo type nameThis means that we cannot explicitly reference this type of name. In fact, it is declared by the compiler in the background and helps you generate the necessary code.
Code
class Program
{
public static void Main(string[] args)
{
var T1 = new {Index = 10,Name = "CPU",Price = 200.0};
var T2 = new {Index = 20,Name = "MethodBoard",Price = 499.0};
var T3 = new {T1.Index, Name = "SoundCard",Price = 210.0};
}
}
Above, we use var to declare three implicit types of variables T1, T2, and T3, and assign them the three anonymous instances created (new. Here var is equivalent to a placeholder. The specific types of its variables (T1, T2, T3) are determined during code compilation, that is, it depends on the Data Type of the values assigned to them. Therefore, T1, T2, and T3 have a specific type in the final generated pencil, that isIs strongly typed
In the above example, we can see that T1's attribute Index is used in T3, which indicates that anonymous attributes are completely accessible. They use the same property name, the Data Type of the property is also the same, andThe order of attributes is also consistent.So they are compatible. Otherwise, they are not compatible.
For example, the following are not the same types. The T4 and T5 attributes are different, and the order of T4 and T6 attributes is different.
var T4 = new {Index = 10,Name = "CPU",Price = 200.0};
var T5 = new {Index = 20,Title = "MethodBoard",Price = 499.0};
var T6 = new {T1.Index, Price = 210.0, Name = "SoundCard" };
The anonymous type is "immutable". That is to say, an anonymous instance cannot change its attributes. Otherwise, compilation errors may occur, for example:
var T4 = new {Index = 10,Name = "CPU",Price = 200.0};T4.Index