473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

.NET 2.0 generic.list.IndexOf doesn't call CompareTo of contained class


I'm trying to implement strongly typed lists in the 2.0 framework. I'm
using VS2005 beta 2. So far, System.Collections.Generic.List appears to
be the ideal solution. However, the generic.List.IndexOf function
doesn't appear to be invoking the contained class' CompareTo method. My
understanding is that it should.

The contained class (IssStruct) implements the IComparable and
IComparable<T> interfaces.

However, the List.Sort function does invoke CompareTo, which suggests
that the syntax for the IComparable interface is correct. The same
problem occurs when the class in contained in an ArrayList.

I'm a rank beginner, so feel free to point out anything obvious.

Below are the class declarations. IssStructs is a generic list that
contains IssStruct class instances; UnsafeIssStructs is an ArrayList
that contains IssStruct class instances. They behave the same. IndexOf
does not invoke IssStruct.CompareTo, Sort does invoke
IssStruct.CompareTo.

Muchos thanks to anyone who can offer help.

namespace ClassLibrary1
{

// this is the contained class
public class IssStruct:IComparable<IssStruct>, IComparable
{
public string fStructName;

public IssStruct(string iStructName)
{
fStructName = iStructName;
}

// non-generic version of CompareTo
public int CompareTo(object obj)
{
return fStructName.CompareTo(((IssStruct)obj).fStructName );
}
// generic version of CompareTo
public int CompareTo(IssStruct obj)
{
return fStructName.CompareTo(obj.fStructName);
}
} // end IssStruct, the contained class

// container class. "Has a..." list, not an "Is a..." list
public class IssStructList
{
public List<IssStruct> IssStructs;
public ArrayList UnSafeIssStructs;
public IssStructList()
{
IssStructs = new List<IssStruct>();
UnSafeIssStructs = new ArrayList();
}
}
} // end namespace ClassLibrary1

Nov 17 '05 #1
5 5382
On 5 Aug 2005 15:06:56 -0700, "majm" <ba********@earthlink.net> wrote:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm
using VS2005 beta 2. So far, System.Collections.Generic.List appears to
be the ideal solution. However, the generic.List.IndexOf function
doesn't appear to be invoking the contained class' CompareTo method. My
understanding is that it should.


No, it should and does invoke Equals (inherited from Object if not
defined on your element type). IndexOf only needs equality, not
ordering, hence Equals rather than CompareTo.

If you want to do a search that uses the ordering established by
CompareTo you need to call first Sort, then BinarySearch, rather than
IndexOf. BinarySearch will invoke CompareTo to find an element.
--
http://www.kynosarges.de
Nov 17 '05 #2

"Christoph Nahr" <ch************@kynosarges.de> wrote in message
news:er********************************@4ax.com...
On 5 Aug 2005 15:06:56 -0700, "majm" <ba********@earthlink.net> wrote:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm
using VS2005 beta 2. So far, System.Collections.Generic.List appears to
be the ideal solution. However, the generic.List.IndexOf function
doesn't appear to be invoking the contained class' CompareTo method. My
understanding is that it should.


No, it should and does invoke Equals (inherited from Object if not
defined on your element type). IndexOf only needs equality, not
ordering, hence Equals rather than CompareTo.

If you want to do a search that uses the ordering established by
CompareTo you need to call first Sort, then BinarySearch, rather than
IndexOf. BinarySearch will invoke CompareTo to find an element.


The docs, however, say that IndexOf uses CompareTo. I've fired off an email
to see if I can find anything out. Digging around it appears that IndexOf
will rely on IEquatable
You've got me. What version of MSDN help are you using?
Nov 17 '05 #3
On Sat, 6 Aug 2005 04:16:38 -0500, "Daniel O'Connell [C# MVP]"
<onyxkirx@--NOSPAM--comcast.net> wrote:
The docs, however, say that IndexOf uses CompareTo. I've fired off an email
to see if I can find anything out. Digging around it appears that IndexOf
will rely on IEquatable


Now that you mention it, I do recall that there was such an error in
the Collections docs in one beta version. It's fixed in the current
July CTP, though.

IEquatable defaults to Object.Equals, by the way, although explicitly
implementing the interface should provide better performance for value
types I guess.
--
http://www.kynosarges.de
Nov 17 '05 #4

"Christoph Nahr" <ch************@kynosarges.de> wrote in message
news:qv********************************@4ax.com...
On Sat, 6 Aug 2005 04:16:38 -0500, "Daniel O'Connell [C# MVP]"
<onyxkirx@--NOSPAM--comcast.net> wrote:
The docs, however, say that IndexOf uses CompareTo. I've fired off an
email
to see if I can find anything out. Digging around it appears that IndexOf
will rely on IEquatable
Now that you mention it, I do recall that there was such an error in
the Collections docs in one beta version. It's fixed in the current
July CTP, though.


Ya,that appears to be the case. I'm still using beta2 due to my extreme
laziness.

IEquatable defaults to Object.Equals, by the way, although explicitly
implementing the interface should provide better performance for value
types I guess.


Ya, it does seem a little superflous at points, but an examination of the
class shows that that is what it does. I don't recall having ever heard of
it, somehow.
Nov 17 '05 #5
Thanks, gentlemen. IEquatable did it.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kamilche | last post by:
I've written a generic sort routine that will sort dictionaries, lists, or tuples, either by a specified key or by value. Comments welcome! import types def sort(container, key = None,...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
2
by: Greg Buchholz | last post by:
/* I've been experimenting with some generic/polytypic programs, and I've stumbled on to a problem that I can't quite figure out. In the program below, I'm trying to define a generic version of...
6
by: SharpCoderMP | last post by:
hi, until .net 2.0 i've been writing my own collections derived from CollectionBase. Now with the generics it is much easier to do that... but! i have some questions about generic lists of...
4
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my...
3
by: Peter Olcott | last post by:
How does not specify the sort criteria for Generic.List ?? The way that this is done in C++ STL is to implement operator<(), how is this done in C# and DotNet for Generic.List ???
1
by: raylopez99 | last post by:
I seem to get name collision between the Generic collection SortedList and C++.NET Framework collection SortedList. How to resolve? Here are the libraries that seem to clash:...
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
5
by: Michi Henning | last post by:
I can pass a generic collection as ICollection<Tjust fine: static void flatCollection(ICollection<intc) {} // ... List<intl = new List<int>(); flatCollection(l); // Works fine Now I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.