473,378 Members | 1,441 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,378 software developers and data experts.

Testing for implementation if IComparable<T> regarless of <T>


You can you program against generic interfaces generically?

For example, how can I make the following code which works for the
non-generic interface also work for the generic counterparts?

public bool Equivalent(ArrayList x, ArrayList y) {
if (x[0] is IComparable) {
x.Sort();
x.Sort();
}
...
}

The above does not work with generic counterparts because
IComparable<Tdoes not extend IComparable (similarly IList<does not
extend IList even though List<implements both, ditto for
IDictionary<vs IDictionary).

As a workaround I know I can always call Sort() and catch the error
that's thrown when the contained items are not IComparable, but this
issue of dealing with generics when the parameterized type is not
known has come up before.

Thanks,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

May 15 '07 #1
3 2608
Hi,

I do not understand your question completely, so I will make a guess.

First you call Sort twice in X (may be a typo).
Also what is the point in seeing if x[0] is comparable? ArrayList can
contain several types and x[1] could not implement IComparable.
The same occur with y.

"Samuel R. Neff" <sa********@nomail.comwrote in message
news:l6********************************@4ax.com...
>
You can you program against generic interfaces generically?

For example, how can I make the following code which works for the
non-generic interface also work for the generic counterparts?

public bool Equivalent(ArrayList x, ArrayList y) {
if (x[0] is IComparable) {
x.Sort();
x.Sort();
}
...
}

The above does not work with generic counterparts because
IComparable<Tdoes not extend IComparable (similarly IList<does not
extend IList even though List<implements both, ditto for
IDictionary<vs IDictionary).

As a workaround I know I can always call Sort() and catch the error
that's thrown when the contained items are not IComparable, but this
issue of dealing with generics when the parameterized type is not
known has come up before.

Thanks,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

May 15 '07 #2

Yes, it's possible that the ArrayList will have different object
types. I happen to know it will never happen in my app, and even
better with generics we can guarantee it won't happen.

Yes the double x.Sort() call was a typo--intended to be x.Sort() and
y.Sort().

Sam

On Tue, 15 May 2007 10:11:08 -0400, "Ignacio Machin \( .NET/ C# MVP
\)" <machin TA laceupsolutions.comwrote:
>Hi,

I do not understand your question completely, so I will make a guess.

First you call Sort twice in X (may be a typo).
Also what is the point in seeing if x[0] is comparable? ArrayList can
contain several types and x[1] could not implement IComparable.
The same occur with y.

"Samuel R. Neff" <sa********@nomail.comwrote in message
news:l6********************************@4ax.com.. .
>>
You can you program against generic interfaces generically?

For example, how can I make the following code which works for the
non-generic interface also work for the generic counterparts?

public bool Equivalent(ArrayList x, ArrayList y) {
if (x[0] is IComparable) {
x.Sort();
x.Sort();
}
...
}
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
May 15 '07 #3
I wouldn't personally get much into the realms of mising generic and
non-generic like this, but in the generic (i.e.
System.Collections.Generic) sense, Comparer<T>.Default will check for
a range of ways for comparing T, returning an IComparer<T>:
* checks for IComparable<T>
* same, using Nullable<T>
* then switches to Comparer.Default:
* checks for equality / nulls
* special case for strings
* checks for IComparable
* throws

So using Comparar<T>.Default is a good choice if you don't know T in
advance. Not sure how much of that is documented and guaranteed, but
that's what it does in 2.0 ;-p

Marc
May 15 '07 #4

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

Similar topics

11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
9
by: Simple Simon | last post by:
Java longs are 8 bytes. I have a Java long that is coming in from the network, and that represents milliseconds since Epoch (Jan 1 1970 00:00:00). I'm having trouble understanding how to get it...
4
by: Michel Walsh | last post by:
Question: public static Boolean In<T>(T x, param T y) where T : struct, IComparable<T> { for(int i=0; i<y.Length; i++) { if(x==y) return true; } return false;
4
by: =?Utf-8?B?TGFycnlS?= | last post by:
I need some help with a multilevel sorting problem with the List<>. I have a List<ItemToSort( see below ) that needs to be sorted in the following manner: Sort by Level1Id ( ok that was the easy...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
2
by: Savior | last post by:
then find out what is T?
3
by: TonyJ | last post by:
Hello! I'm reading in a book from Microsoft that the generic interface IComparabler<Tdefine the two methods CompareTo and Equals. Now to my querstion if I look in the docs it says that...
1
by: Tony Johansson | last post by:
Hello!¨ I read a book and here it says that it's most often an advantage to implement the generic interface IComparable<T> instead of the general interface IComparable. This is correct! Now...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.