Udostępnij za pośrednictwem


Anonymous Type Definition

In response to the declaration of an instance of an anonymous type, the compiler creates a new class definition that contains the specified properties for the type.

Compiler-Generated Code

For the following definition of product, the compiler creates a new class definition that contains properties Name, Price, and OnHand.

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

The class definition contains property definitions similar to the following. Notice that there is no Set method for the key properties. The values of key properties are read-only.

Public Class $Anonymous1
    Private _name As String
    Private _price As Double
    Private _onHand As Integer
     Public Property Name() As String
        Get
            Return _name
        End Get
    End Property

    Public Property Price() As Double
        Get
            Return _price
        End Get
    End Property

    Public Property OnHand() As Integer
        Get
            Return _onHand
        End Get
        Set(ByVal Value As Integer)
            _onHand = Value
        End Set
    End Property

End Class

In addition, anonymous type definitions contain a default constructor. Constructors that require parameters are not permitted.

If an anonymous type declaration contains at least one key property, the type definition overrides three members inherited from Object: Equals, GetHashCode, and ToString. If no key properties are declared, only ToString is overridden. The overrides provide the following functionality:

  • Equals returns True if two anonymous type instances are the same instance, or if they meet the following conditions:

    • They have the same number of properties.

    • The properties are declared in the same order, with the same names and the same inferred types. Name comparisons are not case-sensitive.

    • At least one of the properties is a key property, and the Key keyword is applied to the same properties.

    • Comparison of each corresponding pair of key properties returns True.

    For example, in the following examples, Equals returns True only for employee01 and employee08. The comment before each line specifies the reason why the new instance does not match employee01.

    Dim employee01 = New With {Key .Name = "Bob", Key .Category = 3, .InOffice = False}
    
    ' employee02 has no InOffice property. 
    Dim employee02 = New With {Key .Name = "Bob", Key .Category = 3}
    
    ' The first property has a different name. 
    Dim employee03 = New With {Key .FirstName = "Bob", Key .Category = 3, .InOffice = False}
    
    ' Property Category has a different value. 
    Dim employee04 = New With {Key .Name = "Bob", Key .Category = 2, .InOffice = False}
    
    ' Property Category has a different type. 
    Dim employee05 = New With {Key .Name = "Bob", Key .Category = 3.2, .InOffice = False}
    
    ' The properties are declared in a different order. 
    Dim employee06 = New With {Key .Category = 3, Key .Name = "Bob", .InOffice = False}
    
    ' Property Category is not a key property. 
    Dim employee07 = New With {Key .Name = "Bob", .Category = 3, .InOffice = False}
    
    ' employee01 and employee 08 meet all conditions for equality. Note  
    ' that the values of the non-key field need not be the same. 
    Dim employee08 = New With {Key .Name = "Bob", Key .Category = 2 + 1, .InOffice = True}
    
    ' Equals returns True only for employee01 and employee08.
    Console.WriteLine(employee01.Equals(employee08))
    
  • GetHashcode provides an appropriately unique GetHashCode algorithm. The algorithm uses only the key properties to compute the hash code.

  • ToString returns a string of concatenated property values, as shown in the following example. Both key and non-key properties are included.

    Console.WriteLine(employee01.ToString())
    Console.WriteLine(employee01)
    ' The preceding statements both display the following: 
    ' { Name = Bob, Category = 3, InOffice = False }
    

Explicitly named properties of an anonymous type cannot conflict with these generated methods. That is, you cannot use .Equals, .GetHashCode, or .ToString to name a property.

Anonymous type definitions that include at least one key property also implement the System.IEquatable<T> interface, where T is the type of the anonymous type.

Note

Anonymous type declarations create the same anonymous type only if they occur in the same assembly, their properties have the same names and the same inferred types, the properties are declared in the same order, and the same properties are marked as key properties.

See Also

Tasks

How to: Declare an Instance of an Anonymous Type

How to: Infer Property Names and Types in Anonymous Type Declarations

Concepts

Anonymous Types

Named and Anonymous Types Compared