本文整理汇总了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.