本文整理汇总了C#中System.Collections.Hashtable.Hashtable构造函数的典型用法代码示例。如果您正苦于以下问题:C# Hashtable构造函数的具体用法?C# Hashtable怎么用?C# Hashtable使用的例子?那么恭喜您, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在类System.Collections.Hashtable
的用法示例。
在下文中一共展示了Hashtable构造函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Equals
//引入命名空间
using System;
using System.Collections;
using System.Globalization;
class myComparer : IEqualityComparer
{
public new bool Equals(object x, object y)
{
return x.Equals(y);
}
public int GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
return myComparer.Compare(x, y) == 0;
}
public int GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
var myHT1 = 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.
var myHT2 = 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.
Hashtable myHT3 = 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".
var myCul = new CultureInfo("tr-TR");
var myHT4 = 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")}");
}
}
输出:
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
//引入命名空间
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(3, .8f);
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.
Hashtable myHT2 = new Hashtable(3, .8f, 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".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(3, .8f, 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"));
}
}
输出:
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
//引入命名空间
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create the dictionary.
SortedList mySL = new SortedList();
mySL.Add("FIRST", "Hello");
mySL.Add("SECOND", "World");
mySL.Add("THIRD", "!");
// Create a hash table using the default comparer.
Hashtable myHT1 = new Hashtable(mySL, .8f);
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
Hashtable myHT2 = new Hashtable(mySL, .8f,
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".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = new Hashtable(mySL, .8f, 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"));
}
}
输出:
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
//引入命名空间
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create a hash table using the default comparer.
Hashtable myHT1 = 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.
Hashtable myHT2 = 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".
CultureInfo myCul = new CultureInfo("tr-TR");
Hashtable myHT3 = 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"));
}
}
输出:
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
//引入命名空间
using System;
using System.Collections;
using System.Globalization;
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
public new bool Equals(object x, object y)
{
return myComparer.Compare(x, y) == 0;
}
public int GetHashCode(object obj)
{
// Compare the hash code for the lowercase versions of the strings.
return obj.ToString().ToLower().GetHashCode();
}
}
public class SamplesHashtable
{
public static void Main()
{
// Create the dictionary.
var mySL = new SortedList();
mySL.Add("FIRST", "Hello");
mySL.Add("SECOND", "World");
mySL.Add("THIRD", "!");
// Create a hash table using the default comparer.
var myHT1 = new Hashtable(mySL);
// Create a hash table using the specified IEqualityComparer that uses
// the CaseInsensitiveComparer.DefaultInvariant to determine equality.
var myHT2 = 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".
var myCul = new CultureInfo("tr-TR");
var myHT3 = 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")}");
}
}
输出:
Results vary depending on the system's culture settings. first is in myHT1: False first is in myHT2: True first is in myHT3: False