470,849 Members | 1,300 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Dictionary<K,V> and Case Insensitive key compare?

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.


Note: Also posted this at microsoft.private.whidbey.generics, but after the
fact figured I would get more joy here.

William Stacey, MVP

William Stacey, MVP
Nov 16 '05 #1
0 7574

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Andrew Robinson | last post: by
4 posts views Thread by Peter K | last post: by
2 posts views Thread by =?Utf-8?B?YW1pcg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.