C # differences between the anonymous type and the VB anonymous type

Source: Internet
Author: User

1. Anonymous type in C #

The anonymous type is a new feature introduced in C #3.0 (framework3.5. As the name implies, an anonymous type is an object without a type name, which inherits directly from the object.

The C # anonymous type can be defined in two ways:

// 1. directly declare the Member and initialize func <int, int> fun = x => x + 1; var ann1 = new {A = "str ", B = new object (), c = fun // C # You can use a delegate, but you cannot assign a Lambda expression to a member directly, for example, c = x =>{ x + 1 ;}; // 2. Do not declare the member name. The Member is from another object var ANN2 = new {ann1.a, ann1. B }; string STR = ann2.a; ann2.a = "str2"; // It is not allowed to assign values to anonymous type members in C #. Therefore, this line of code will cause an error during compilation: Object TMP = ann2. B; TMP = "str2"; // However, assign the reference type in the anonymous method to another variable and assign the value to the variable, the allowed int T = ann1.c (1) is used to change the value of an anonymous member );

  

Ii. Anonymous type in VB

The anonymous type in VB is more flexible than that in C. Anonymous object members can be defined as read-only or writable, and anonymous methods are supported. Let's take a look at the example below.

'Define writable anonymous type dim ann1 = new {. A = "",. B = new object (),. C = function (byref X) as integer return x + 9 end function} 'Use the key keyword to define read-only anonymous members. In the following definition, member A is read-only, B is writable dim ANN2 = new with {key. A = "", ann1. B} ann1.a = "str" 'the anonymous type defined without the key keyword is writable ann2.a = "str"' the anonymous type defined with the key keyword is read-only, the error dim TMP as integer = ann1.c (1) is prompted during compilation)

Of course, the key keyword is not used to limit the read-only members, but to generate a hash value for an anonymous instance to compare whether two anonymous objects are the same object.

For example:

        Dim ann3 = New With {Key .A = "a",                           .B = "e"                           }        Dim ann4 = New With {Key .A = "a",                             .B = "3"                            }        If (ann3.Equals(ann4)) Then            ‘ ToDo........        End If

The above ann3 and ann4 are equal, because they are instances of the same anonymous type (so-called instances of the same anonymous type, only two members of the same anonymous type have the same name and member type ), and the key property has equal values.

C # differences between the anonymous type and the VB anonymous type

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.