Share via


匿名型別定義 (Visual Basic)

遇到匿名型別執行個體 (Instance) 的宣告時,編譯器 (Compiler) 會建立新的類別定義,其中含有指定給該型別的屬性。

編譯器產生的程式碼

根據下列 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

此外,匿名型別定義中也包含預設建構函式 (Constructor)。 不允許有需要使用參數的建構函式。

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

  • 如果兩個匿名型別執行個體是同一個執行個體,或符合下列條件,則 Equals 會傳回 True:

    • 具有相同數目的屬性。

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

    • 至少有一個屬性是索引鍵屬性,而且套用 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 來命名屬性。

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

注意事項注意事項

匿名型別宣告只有在下列情況下才會建立相同的匿名型別:在同一個組件 (Assembly) 中宣告、其屬性具有相同的名稱和推斷型別、屬性的宣告順序相同,以及將相同的屬性標記為索引鍵屬性。

請參閱

工作

HOW TO:宣告匿名型別的執行個體 (Visual Basic)

HOW TO:在匿名型別宣告中推斷屬性名稱和型別 (Visual Basic)

概念

匿名型別 (Visual Basic)

具名和匿名型別比較 (Visual Basic)