共用方式為


匿名類型定義 (Visual Basic)

為了回應匿名類型實例的宣告,編譯器會建立一個新的類別定義來包含該類型的指定屬性。

編譯器產生的程式碼

針對 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 只會針對 employee01employee08 傳回 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 來命名屬性。

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

注意

只有當匿名類型宣告在相同的組件中進行、其屬性具有相同的名稱和相同的推斷類型、屬性以相同順序宣告,以及將相同的屬性標示為索引鍵屬性時,匿名類型宣告才會建立相同的匿名類型。

另請參閱