次の方法で共有


匿名型定義 (Visual Basic)

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

Compiler-Generated コード

次の 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 のみがオーバーライドされます。 オーバーライドにより、次の機能が提供されます。

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

    • プロパティの数は同じです。

    • プロパティは、同じ名前と同じ推論型を使用して、同じ順序で宣言されます。 名前の比較では、大文字と小文字は区別されません。

    • 少なくとも 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 は匿名型の型です。

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

こちらも参照ください