Hashtable忽略大小写
在.net1.1中这样写
Hashtable htTemp = new Hashtable( new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer() );
到了,.net2.0后提示过期
所以可以像下面这样写。
class myCultureComparer : IEqualityComparer
{
#region 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)
{
return obj.ToString().ToLower().GetHashCode();
}
#endregion
}
{
#region 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)
{
return obj.ToString().ToLower().GetHashCode();
}
#endregion
}
使用的时候
Hashtable htTemp1 = new Hashtable(new myCultureComparer());