Anonymous and implicit type variables in C #

Source: Internet
Author: User

 

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
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.