6. Anonymous Types (Anonymous type)

Source: Internet
Author: User

Return directory]

In C #3.0, the keyword new and the anonymous object initiator are used together to create an anonymous object. This sentence seems abstract, but it is easy to understand as long as there are specific examples, let's take a look at what the anonymous type is best for our programmers:

   1: var anonymousObject = new { ID = 1, Name = "Object" };

The code above declares an anonymous object. We can see that the type declaration of anonymousObject is var, that is, it is an implicit type during declaration, the object initiator provides two attributes, ID and Name, with the initial values assigned respectively, now we can regard anonymousObject as a user-defined data type similar to StudentData in the previous article.

The anonymous declaration method is as simple as this, so let's take a look at the effect of the following code:

   1: static void Main(string[] args)
   2: {
   3:     var anonymousObjectA = new { ID = 1, Name = "Object" };
   4:     var anonymousObjectB = new { ID = 100, Name = "Anonymous" };
   5:  
   6:     anonymousObjectB = anonymousObjectA;
   7:  
   8:     Console.WriteLine(anonymousObjectB.Name + "'s ID is " + anonymousObjectB.ID + ".");
   9:     Console.ReadLine();
  10: }

There will be no errors in this Code, because anonymousObjectA and anonymousObjectB belong to the same type. The anonymous type rule is: as long as the names and sequences of the attributes specified in the anonymous object initiator are consistent, the anonymous objects belong to the same anonymous type. Therefore, the output result of the above code is "Object's ID is 1 .". The last thing to note is that different objects of the same anonymous type only have the same attribute values. For example, if the attribute values of anonymousObjectA are the same as those of anonymousObjectB, anonymousObjectA. equals (anonymousObjectB) is True, and their GetHashCode () obtains the same value.

Next, let's take a look at the Query Expressions (Query expression), the last new feature of C #3.0 ).

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.