20.2.1 introduces anonymous variables
In many cases, we need an object that can temporarily store a batch of data with certain associations, or, in some cases, we are more interested in the "shape" of only one object, such as the name and type of the attribute. For example, book class, when it is queried with other items, we may only be interested in its name and price, and would like to place both attributes in a separate temporary object for future use. At this point, we are concerned only that this temporary object has attributes of name and price, and it doesn't matter what type it is. However, in order for such an object to exist, we have to write a lot of "sample code" for this insignificant type, which is to define a class such as Bookasgood, which is nothing more than a private domain like M_name and m_price. and a public read-write method named name and price. The code looks like this:
public class BookAsGood
{
// 定义一组私有成员变量
private string m_name;
private double m_price;
// 为成员变量设置属性
public string Name
{
get
{
return this.m_name;
}
set
{
this.m_name = value;
}
}
public string Price
{
get
{
return this.m_price;
}
set
{
this.m_price = value;
}
}
}
If too many members are packaged like this, the amount of code is terrible, and the amount of maintenance work is quite large. For these issues, in C # 3.0, we have a shortcut to address this problem, called an anonymous type, which is an extension of C # anonymous method syntax.