共用方式為


匿名類型定義 (Visual Basic)

為了回應匿名型別實例的宣告,編譯程式會建立新的類別定義,其中包含型別的指定屬性。

Compiler-Generated 程式代碼

針對的下列定義 product,編譯程式會建立新的類別定義,其中包含屬性 NamePriceOnHand

' 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

此外,匿名類型定義也包含無參數建構函式。 不允許需要參數的建構函式。

如果匿名型別宣告包含至少一個索引鍵屬性,則類型定義會覆寫繼承自Object的三個成員:EqualsGetHashCodeToString。 如果沒有宣告任何關鍵屬性,則只會覆寫 ToString。 覆寫提供下列功能:

  • Equals True如果兩個匿名型別實例是相同的實體,或符合下列條件,則傳回 :

    • 它們具有相同數量的屬性。

    • 屬性會以相同順序宣告,且具有相同的名稱和相同的推斷型別。 名稱比較不區分大小寫。

    • 至少有一個屬性為關鍵屬性,且 Key 關鍵詞會被應用於這些相同的屬性。

    • 對每一對應的關鍵屬性進行比較時將傳回True

      例如,在下列範例中,Equals只會針對 Trueemployee01employee08回 。 每個行前面的批注會指定新實例不符合 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 來命名屬性。

包含至少一個鍵屬性的匿名型別定義也會實作介面 System.IEquatable<T>,其中 T 是匿名型別的類型。

備註

只有在相同的元件中發生匿名類型時,匿名型別宣告才會建立相同的匿名型別、其屬性具有相同的名稱和相同的推斷型別、屬性會以相同順序宣告,而且相同的屬性會標示為索引鍵屬性。

另請參閱