本文整理匯總了VB.NET中System.Collections.Hashtable.Hashtable構造函數的典型用法代碼示例。如果您正苦於以下問題:VB.NET Hashtable構造函數的具體用法?VB.NET Hashtable怎麽用?VB.NET Hashtable使用的例子?那麽, 這裏精選的構造函數代碼示例或許可以為您提供幫助。您也可以進一步了解該構造函數所在類System.Collections.Hashtable
的用法示例。
在下文中一共展示了Hashtable構造函數的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: myComparer
' 導入命名空間
Imports System.Collections
Imports System.Globalization
Public Class myComparer
Implements IEqualityComparer
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return x.Equals(y)
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return myComparer.Compare(x, y) = 0
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable()
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the default Object.Equals to determine equality.
Dim myHT2 As New Hashtable(New myComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using a case-insensitive hash code provider and
' case-insensitive comparer based on the InvariantCulture.
Dim myHT3 As New Hashtable( _
CaseInsensitiveHashCodeProvider.DefaultInvariant, _
CaseInsensitiveComparer.DefaultInvariant)
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT4 As New Hashtable(New myCultureComparer(myCul))
myHT4.Add("FIRST", "Hello")
myHT4.Add("SECOND", "World")
myHT4.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}")
End Sub
End Class
輸出:
Results vary depending on the system's culture settings. first is in myHT1: False first is in myHT2: False first is in myHT3: True first is in myHT4: False
示例2: myCultureComparer
' 導入命名空間
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(3, System.Convert.ToSingle(0.8))
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(3, System.Convert.ToSingle(0.8), _
New myCultureComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(3, System.Convert.ToSingle(0.8), _
New myCultureComparer(myCul))
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
輸出:
Results vary depending on the system's culture settings. first is in myHT1: False first is in myHT2: True first is in myHT3: False
示例3: myCultureComparer
' 導入命名空間
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create the dictionary.
Dim mySL As New SortedList()
mySL.Add("FIRST", "Hello")
mySL.Add("SECOND", "World")
mySL.Add("THIRD", "!")
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(mySL, System.Convert.ToSingle(0.8))
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
New myCultureComparer())
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
New myCultureComparer(myCul))
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
輸出:
Results vary depending on the system's culture settings. first is in myHT1: False first is in myHT2: True first is in myHT3: False
示例4: myCultureComparer
' 導入命名空間
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(3)
myHT1.Add("FIRST", "Hello")
myHT1.Add("SECOND", "World")
myHT1.Add("THIRD", "!")
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(3, New myCultureComparer())
myHT2.Add("FIRST", "Hello")
myHT2.Add("SECOND", "World")
myHT2.Add("THIRD", "!")
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(3, New myCultureComparer(myCul))
myHT3.Add("FIRST", "Hello")
myHT3.Add("SECOND", "World")
myHT3.Add("THIRD", "!")
' Search for a key in each hash table.
Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
End Sub
End Class
輸出:
Results vary depending on the system's culture settings. first is in myHT1: False first is in myHT2: True first is in myHT3: False
示例5: myCultureComparer
' 導入命名空間
Imports System.Collections
Imports System.Globalization
Public Class myCultureComparer
Implements IEqualityComparer
Dim myComparer As CaseInsensitiveComparer
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
Public Sub New(ByVal myCulture As CultureInfo)
myComparer = New CaseInsensitiveComparer(myCulture)
End Sub
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
Return myComparer.Compare(x, y) = 0
End Function
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
End Class
Public Class SamplesHashtable
Public Shared Sub Main()
' Create the dictionary.
Dim mySL As New SortedList()
mySL.Add("FIRST", "Hello")
mySL.Add("SECOND", "World")
mySL.Add("THIRD", "!")
' Create a hash table using the default comparer.
Dim myHT1 As New Hashtable(mySL)
' Create a hash table using the specified IEqualityComparer that uses
' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Dim myHT2 As New Hashtable(mySL, New myCultureComparer())
' Create a hash table using an IEqualityComparer that is based on
' the Turkish culture (tr-TR) where "I" is not the uppercase
' version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim myHT3 As New Hashtable(mySL, New myCultureComparer(myCul))
' Search for a key in each hash table.
Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
End Sub
End Class
輸出:
Results vary depending on the system's culture settings. first is in myHT1: False first is in myHT2: True first is in myHT3: False