Is CaseInsensitiveComparer Obsolete??

G

Guest

I am reading the MCTS Exam 70-536 book by Nortrop and Wildermuth. Chapter 4
discusses the IEQualityComparer interface, and shows how to use it for a
HashTable. The August06 MSDN docs also show a similar example. In these
examples the class used for the IEQualityComparer has a
CaseInsensitiveComparer field. I found that when calling the
CaseInsensitiveComparer in the Equals method that it just calls the CompareTo
method in the class that I am using for the key of my HashTable. So, I am
wondering what CaseInsensitiveComparer is supposed to do if it really just
calls your own method to do the compare.

In my method I use one of the String.Toxxxxx methods (like
ToLowerInvariant()), which is new for .NET 2.0. So, I modified my Equals
method to just directly call my CompareTo method and not use
CaseInsensitiveComparer at all. It has the same result.

So, it looks like CaseInsensitiveComparer is obsolete, but is still
supported to not break older code, even though it now requires your key class
to implement IComparable to provide the CompareTo method, that would use one
of the new String.Toxxxx methods to do the actual case insensitive comparison
using a cultureinfo.

The book and MSDN help files make no mention of the things I found (the hard
way of course).

A secondary question, that then just naturally occurs to me, is "Why is
Microsoft's documentation and MSPress books so bad when it comes to helping
people learn .NET 2.0???" The book contains MANY more errors, and there is
nothing available from MS to help anyone. There is no way you could learn
this from the MSDN stuff, which is geared towards helping developers remember
how to use it than to teach them in the first place? Why would MS invest
billions to develop .NET and yet refuse to spend thousands to make it easier
for people to learn and understand?

Oh, and I think they should stop forcing their programmers (like Tony
Northrop) to write books, unless they are going to fully support that effort.
Free advice: pay Jeff Richter what ever he wants to write updated books that
fully cover .NET 2.0!
 
L

Larry Lard

DXRick said:
I am reading the MCTS Exam 70-536 book by Nortrop and Wildermuth. Chapter 4
discusses the IEQualityComparer interface, and shows how to use it for a
HashTable. The August06 MSDN docs also show a similar example. In these
examples the class used for the IEQualityComparer has a
CaseInsensitiveComparer field. I found that when calling the
CaseInsensitiveComparer in the Equals method that it just calls the CompareTo
method in the class that I am using for the key of my HashTable. So, I am
wondering what CaseInsensitiveComparer is supposed to do if it really just
calls your own method to do the compare.

In my method I use one of the String.Toxxxxx methods (like
ToLowerInvariant()), which is new for .NET 2.0. So, I modified my Equals
method to just directly call my CompareTo method and not use
CaseInsensitiveComparer at all. It has the same result.

So, it looks like CaseInsensitiveComparer is obsolete, but is still
supported to not break older code, even though it now requires your key class
to implement IComparable to provide the CompareTo method, that would use one
of the new String.Toxxxx methods to do the actual case insensitive comparison
using a cultureinfo.

You might be on to something here, but it's really hard to tell. Could
you just say:

- What you did
- What you expected to see
- What actually happened

?
 
G

Guest

I found that it is a bit more complicated. I was using a custom class to
wrap a string and using that class as the key for the non-generic HashTable
class. I found that it the CaseInsensitiveComparer was calling my CompareTo
method, because I was passing a reference to my class to it and not the
embeded string. When I passed the strings, it worked the same without
calling my method.

Two things threw me here. Firstly, the CaseInsensitiveHashCodeProvider
class is marked as obsolete, and constructors that take one must take both.
For example:

Hashtable myHT2 = new Hashtable( new CaseInsensitiveHashCodeProvider(),
new CaseInsensitiveComparer() );

Secondly, the String class now has those new Toxxxx methods for doing the
same types of comparisons.

Also, I am reading Richter's CLR via C# book, that gets you thinking about
efficiency.

So, it seemed very inconsistent that one was obsolete and the other was not,
and it bothered me that using the CaseInsensitiveComparer would be less
efficient than using the String methods.

The MSDN docs and the MSPress book make no mention of what I found after
digging around and fiddling with the code. I have been finding similar
issues with other .NET 2.0 classes, where the example code doesn't work at
all or there are better ways to do the same thing.

It frustrates me when I see that MS has done so little to help people learn
how to use .NET 2.0. They are relying on 3rd parties (like Richter) to write
books that explain it better. It makes me wonder how much they really
believe in the future of .NET, when it is so hard to learn.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top