The anonymous variable of the new feature of c#3.0 language

Source: Internet
Author: User
Tags anonymous

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.

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.