W
William Stacey [MVP]
Trying to figure out Dictionary<> and using CaseInsensitive Comparer<> like
I did with normal Hashtable. The Hashtable can take a case insenstive
Comparer and a Case insensitive HashCode provider. It seems the HashCode
provider is lost or not needed in the Generic Dictionary anymore so
wondering if this is how you do the same:
Dictionary<string, Node> nodes = new Dictionary<string, Node>( new
StringCompInsensitive() );
....
public class StringCompInsensitive : IComparer<string>
{
public StringCompInsensitive()
{
}
public int Compare(string x, string y)
{
return String.Compare(x, y, true);
}
public bool Equals(string x, string y)
{
return String.Compare(x, y, true) == 0;
}
public int GetHashCode(string obj)
{
return obj.GetHashCode();
}
}
1) Is this insensitive comparer the best way to implement it? I was
wondering if Equals could be optimized for ref equality or something.
2) Does the Dictionary call Equals first on string and if not equal call
Compare? How does that work or GetHashCode fit in.
3) Do I also need a CaseInsensitive Hash Code provider in GetHashCode or
should this work ok? Need some clarity here.
TIA!
Note: Also posted this at microsoft.private.whidbey.generics, but after the
fact figured I would get more joy here.
I did with normal Hashtable. The Hashtable can take a case insenstive
Comparer and a Case insensitive HashCode provider. It seems the HashCode
provider is lost or not needed in the Generic Dictionary anymore so
wondering if this is how you do the same:
Dictionary<string, Node> nodes = new Dictionary<string, Node>( new
StringCompInsensitive() );
....
public class StringCompInsensitive : IComparer<string>
{
public StringCompInsensitive()
{
}
public int Compare(string x, string y)
{
return String.Compare(x, y, true);
}
public bool Equals(string x, string y)
{
return String.Compare(x, y, true) == 0;
}
public int GetHashCode(string obj)
{
return obj.GetHashCode();
}
}
1) Is this insensitive comparer the best way to implement it? I was
wondering if Equals could be optimized for ref equality or something.
2) Does the Dictionary call Equals first on string and if not equal call
Compare? How does that work or GetHashCode fit in.
3) Do I also need a CaseInsensitive Hash Code provider in GetHashCode or
should this work ok? Need some clarity here.
TIA!
Note: Also posted this at microsoft.private.whidbey.generics, but after the
fact figured I would get more joy here.