C # Anonymous class

Source: Internet
Author: User

<1> the anonymous type provides a convenient method to encapsulate a set of read-only attributes into a single object without explicitly defining a type. The type name is generated by the compiler and cannot be used at the source code level. The type of each attribute is inferred by the compiler.
Example:

Public Curry {public string Age; public string Name; public string Sex ;}// initialization object Curry curry = new Curry {Age = "18", Name = "Andeson ", sex = "femal"}; // or uses the Anonymous class var curry = new {Age = "18", Name = "Andeson", Sex = "femal "};

We can see that new is not followed by the class type, so we define an anonymous class. We don't need to know the name of this class. We can only read this attribute and cannot rewrite the attribute value.
Var Cur = new {m = 100, n = "sdsdfr"}; // Cur. m = 1000; // error, cannot be assigned because it is read-only.

<2> An anonymous class is a class derived directly from an Object.

There is no difference between the anonymous type and other reference types. The only difference is that the anonymous type can only be forcibly converted to the Object type.

<3> if two or more anonymous types have the same number of attribute types in the same dataset, the compiler treats these anonymous types as the same type in the same sequence. They share the type information generated by the same compiler.

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.