本文整理匯總了VB.NET中System.HashCode結構體的典型用法代碼示例。如果您正苦於以下問題:VB.NET HashCode結構體的具體用法?VB.NET HashCode怎麽用?VB.NET HashCode使用的例子?那麽, 這裏精選的結構體代碼示例或許可以為您提供幫助。
在下文中一共展示了HashCode結構體的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: New
Public Structure OrderOrderLine
Implements IEquatable(Of OrderOrderLine)
Public ReadOnly Property OrderId As Integer
Public ReadOnly Property OrderLineId As Integer
Public Sub New(ByVal orderId As Integer, ByVal orderLineId As Integer)
Me.OrderId = orderId
Me.OrderLineId = orderLineId
End Sub
Public Overrides Function Equals(obj As Object) As Boolean
Return (TypeOf obj Is OrderOrderLine) AndAlso Equals(DirectCast(obj, OrderOrderLine))
End Function
Public Overloads Function Equals(other As OrderOrderLine) As Boolean Implements IEquatable(Of OrderOrderLine).Equals
Return OrderId = other.OrderId AndAlso
OrderLineId = other.OrderLineId
End Function
Public Overrides Function GetHashCode() As Integer
Return HashCode.Combine(OrderId, OrderLineId)
End Function
End Structure
Module Program
Sub Main(args As String())
Dim hashSet As HashSet(Of OrderOrderLine) = New HashSet(Of OrderOrderLine)
hashSet.Add(New OrderOrderLine(1, 1))
hashSet.Add(New OrderOrderLine(1, 1))
hashSet.Add(New OrderOrderLine(1, 2))
Console.WriteLine($"Item count: {hashSet.Count}")
End Sub
End Module
輸出:
Item count: 2.
示例2: New
Public Structure Path
Implements IEquatable(Of Path)
Public ReadOnly Property Segments As IReadOnlyList(Of String)
Public Sub New(ParamArray ByVal segments() As String)
Me.Segments = segments
End Sub
Public Overrides Function Equals(obj As Object) As Boolean
Return (TypeOf obj Is Path) AndAlso Equals(DirectCast(obj, Path))
End Function
Public Overloads Function Equals(other As Path) As Boolean Implements IEquatable(Of Path).Equals
If ReferenceEquals(Segments, other.Segments) Then Return True
If Segments Is Nothing OrElse other.Segments Is Nothing Then Return False
If Segments.Count <> other.Segments.Count Then Return False
For i As Integer = 0 To Segments.Count - 1
If Not String.Equals(Segments(i), other.Segments(i)) Then Return False
Next
Return True
End Function
Public Overrides Function GetHashCode() As Integer
Dim hash As HashCode = New HashCode()
For i As Integer = 0 To Segments?.Count - 1
hash.Add(Segments(i))
Next
Return hash.ToHashCode()
End Function
End Structure
Module Program
Sub Main(args As String())
Dim hashSet As HashSet(Of Path) = New HashSet(Of Path) From {
New Path("C:", "tmp", "file.txt"),
New Path("C:", "tmp", "file.txt"),
New Path("C:", "tmp", "file.tmp")
}
Console.WriteLine($"Item count: {hashSet.Count}.")
End Sub
End Module
輸出:
Item count: 2.
示例3: New
Public Structure Path
Implements IEquatable(Of Path)
Public ReadOnly Property Segments As IReadOnlyList(Of String)
Public Sub New(ParamArray ByVal segments() As String)
Me.Segments = segments
End Sub
Public Overrides Function Equals(obj As Object) As Boolean
Return (TypeOf obj Is Path) AndAlso Equals(DirectCast(obj, Path))
End Function
Public Overloads Function Equals(other As Path) As Boolean Implements IEquatable(Of Path).Equals
If ReferenceEquals(Segments, other.Segments) Then Return True
If Segments Is Nothing OrElse other.Segments Is Nothing Then Return False
If Segments.Count <> other.Segments.Count Then Return False
For i As Integer = 0 To Segments.Count - 1
If Not String.Equals(Segments(i), other.Segments(i), StringComparison.OrdinalIgnoreCase) Then Return False
Next
Return True
End Function
Public Overrides Function GetHashCode() As Integer
Dim hash As HashCode = New HashCode()
For i As Integer = 0 To Segments?.Count - 1
hash.Add(Segments(i), StringComparer.OrdinalIgnoreCase)
Next
Return hash.ToHashCode()
End Function
End Structure
Module Program
Sub Main(args As String())
Dim hashSet As HashSet(Of Path) = New HashSet(Of Path) From {
New Path("C:", "tmp", "file.txt"),
New Path("C:", "TMP", "file.txt"),
New Path("C:", "tmp", "FILE.TXT")
}
Console.WriteLine($"Item count: {hashSet.Count}.")
End Sub
End Module
輸出:
Item count: 1.
示例4: New
Public Structure Path
Implements IEquatable(Of Path)
Public ReadOnly Property Segments As IReadOnlyList(Of String)
Public Sub New(ParamArray ByVal segments() As String)
Me.Segments = segments
End Sub
Public Overrides Function Equals(obj As Object) As Boolean
Return (TypeOf obj Is Path) AndAlso Equals(DirectCast(obj, Path))
End Function
Public Overloads Function Equals(other As Path) As Boolean Implements IEquatable(Of Path).Equals
If ReferenceEquals(Segments, other.Segments) Then Return True
If Segments Is Nothing OrElse other.Segments Is Nothing Then Return False
If Segments.Count <> other.Segments.Count Then Return False
For i As Integer = 0 To Segments.Count - 1
If Not PathEquals(Segments(i), other.Segments(i)) Then Return False
Next
Return True
End Function
Public Overrides Function GetHashCode() As Integer
Dim hash As HashCode = New HashCode()
For i As Integer = 0 To Segments?.Count - 1
AddPath(hash, Segments(i))
Next
Return hash.ToHashCode()
End Function
End Structure
Friend Module PlatformUtils
Public Function PathEquals(ByVal a As String, ByVal b As String) As Boolean
Return String.Equals(a, b, StringComparison.OrdinalIgnoreCase)
End Function
Public Sub AddPath(ByRef hash As HashCode, ByVal path As String)
hash.Add(path, StringComparer.OrdinalIgnoreCase)
End Sub
End Module
Module Program
Sub Main(args As String())
Dim hashSet As HashSet(Of Path) = New HashSet(Of Path) From {
New Path("C:", "tmp", "file.txt"),
New Path("C:", "TMP", "file.txt"),
New Path("C:", "tmp", "FILE.TXT")
}
Console.WriteLine($"Item count: {hashSet.Count}.")
End Sub
End Module
輸出:
Item count: 1.