匿名型の定義 (Visual Basic)

匿名型のインスタンスの宣言に応答して、コンパイラでは、型に対して指定されたプロパティを含む新しいクラス定義を作成します。

コンパイラで生成されたコード

次の product の定義では、コンパイラで、NamePrice、および OnHand プロパティを含む新しいクラス定義を作成します。

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

クラス定義には、次のようなプロパティ定義が含まれています。 キー プロパティに Set メソッドがないことに注目してください。 キー プロパティの値は読み取り専用です。

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

    Public ReadOnly 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

また、匿名型の定義には、パラメーターなしのコンストラクターが含まれています。 パラメーターを必要とするコンストラクターは許可されません。

匿名型の定義に少なくとも 1 つのキー プロパティが含まれている場合は、Object から継承された 3 つのメンバー (EqualsGetHashCodeToString) がこの型定義でオーバーライドされます。 キー プロパティが宣言されていない場合は、ToString のみがオーバーライドされます。 オーバーライドでは次の機能が提供されます。

  • 2 つの匿名型のインスタンスが同じインスタンスである場合、または次の条件を満たしている場合、EqualsTrue が返されます。

    • それらのプロパティの数が同じである。

    • プロパティが同じ順序で宣言され、名前と推論された型が同じである。 名前の比較では大文字と小文字は区別されません。

    • 少なくとも 1 つのプロパティがキー プロパティであり、Key キーワードが同じプロパティに適用されている。

    • キー プロパティの対応する各ペアの比較で、True が返される。

      たとえば、次の例では、Equalsemployee01employee08 に対してのみ True が返されます。 各行の前のコメントは、新しいインスタンスが 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 では、適切な一意の GetHashCode アルゴリズムが提供されます。 このアルゴリズムでは、キー プロパティのみを使用してハッシュ コードを計算します。

  • ToString では、次の例に示すように、連結されたプロパティ値の文字列が返されます。 キーとキー以外のプロパティの両方が含まれます。

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

匿名型の明示的に名前が付けられたプロパティは、生成されたこれらのメソッドと競合できません。 つまり、.Equals.GetHashCode.ToString を使用してプロパティの名前を指定することはできません。

少なくとも 1 つのキー プロパティを含む匿名型定義では、System.IEquatable<T> インターフェイスも実装します。ここで、T は匿名型の型です。

Note

匿名型の宣言では、同じアセンブリ内に存在する場合にのみ、同じ匿名型が作成され、そのプロパティの名前と推論される型は同じで、プロパティは同じ順序で宣言され、同じプロパティがキー プロパティとしてマークされます。

関連項目