Udostępnij za pośrednictwem


Named and Anonymous Types Compared (Visual Basic)

The format you use to declare an instance of an anonymous type resembles the format you use when you declare an instance of a named type by using an object initializer. However, there are important differences in the result.

Declarations

In the following examples, product1 is an instance of an anonymous type, and product2 is an instance of class Product. Both declarations use an initialization list in their declarations. The only syntactic difference between the two declarations is that no data type is specified after New for product1. This makes it an anonymous type.

' Variable product1 is an instance of an anonymous type.
Dim product1 = New With {.Name = "paperclips", .Price = 1.29}

Variable product1 is strongly typed as an instance of an anonymous type. The compiler defines the type according to the properties that you specify in the declaration. The compiler uses Local Type Inference (Visual Basic) to determine the data types of the properties from the initial values that you provide. In this example, the anonymous type contains properties Name and Price, and their types are inferred as String and Double, respectively.

Note

In the previous example, none of the properties are specified as key properties. Therefore, the properties play no role in determining whether two instances of the same type are equal or what the hash code value of the instance is. However, the values of the properties can be changed, whereas the values of key properties are read-only. For more information about key properties, see Anonymous Types (Visual Basic).

In the following example, product2 is strongly typed as an instance of the Product class. Both Name and Price must be members of the Product class, and their types must be compatible with the values supplied in the declaration.

' Variable product2 is an instance of Product.
Dim product2 = New Product With {.Name = "paperclips", .Price = 1.29}

For more information, see Object Initializers: Named and Anonymous Types (Visual Basic).

Distinctions

The following list compares other aspects of the two declarations:

  • Local type inference determines that product1 is an instance of an anonymous type and that product2 is an instance of the Product class. There is no As clause in either declaration, although for product2 the As clause is optional.

    • Because anonymous types do not have specified names, you cannot use an As clause to declare product1. Its type must be inferred. This limits the use of anonymous types to local variables.

    • Because product2 is an instance of a named type, Product, you can declare it without using type inference by using an As clause.

      Dim product2a As New Product With {.Name = "paperclips", .Price = 1.29}
      
  • If an appropriate constructor is defined in Product, product2 can be declared and initialized without using an object initializer.

    Dim product2b As New Product("paperclips", 1.29)
    

    Alternatively, the following series of statements has the same effect.

    Dim product2c As New Product
    product2c.Name = "paperclips"
    product2c.Price = 1.29
    

    No similar declarations are possible for product1 because its properties are named and defined in the initializer list itself.

  • Because product1, product2, and their properties are all strongly typed, you can use IntelliSense to navigate through code that uses either object.

    Warning

    The name of the anonymous type is compiler generated, and it may vary from compilation to compilation. Your code should not use or rely on the name of an anonymous type because the name might change when a project is recompiled.

See Also

Tasks

How to: Declare an Instance of an Anonymous Type (Visual Basic)

How to: Infer Property Names and Types in Anonymous Type Declarations (Visual Basic)

Reference

Key (Visual Basic)

Concepts

Anonymous Types (Visual Basic)

Local Type Inference (Visual Basic)