匿名型の定義 (Visual Basic)

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

コンパイラが生成するコード

次の product の定義の場合、コンパイラは、Name、Price、および 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 つのメンバー (EqualsGetHashCode、および ToString) をオーバーライドします。 主要なプロパティの宣言がない場合は、ToString だけがオーバーライドされます。 このオーバーライドには、次の機能があります。

  • Equals は、2 つの匿名型のインスタンスが同一のインスタンスである、またはそれらが以下の条件に適合する場合は True を返します。

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

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

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

    • 対応する主要なプロパティのペアごとの比較で、True が返される。

    たとえば、次の例では、Equals は、employee01 と employee08 に対してのみ 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 は匿名型の型です)。

注意

匿名型の宣言は、それらが同一のアセンブリの中にあり、プロパティの名前と推論された型が同じであり、それらのプロパティが同じ順序で宣言され、同じプロパティが主要なプロパティとマークされている場合のみ、同一の匿名型を作成します。

参照

処理手順

方法: 匿名型のインスタンスを宣言する (Visual Basic)

方法: 匿名型の宣言におけるプロパティ名と型を推論する (Visual Basic)

概念

匿名型 (Visual Basic)

名前付きの型と匿名型の比較 (Visual Basic)