S
Samuel R. Neff
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<T> does 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
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<T> does 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