G
Giulio Petrucci
Hi everybody,
I'm implementing a sort of my own generic collection.
It has a method that returns an int representing the index for a particular
index. If I try to compile the code below:
class MyCollection<T> : ICollection<T> {
T[] array;
MyCollection() {
array = new T[];
}
int GetIndex(T item) {
for (int i=0; i<array.Length; i++) {
if (array==item)
return i;
}
return -1;
}
}
I get this compiler error:
Operator '==' cannot be applied to operands of type 'T' and 'T'
That's because there's no definition for == in T (that's actually a generic
type)?
Thanks,
Giulio
p.s. I solved using array.Equals(item), of course...
I'm implementing a sort of my own generic collection.
It has a method that returns an int representing the index for a particular
index. If I try to compile the code below:
class MyCollection<T> : ICollection<T> {
T[] array;
MyCollection() {
array = new T[];
}
int GetIndex(T item) {
for (int i=0; i<array.Length; i++) {
if (array==item)
return i;
}
return -1;
}
}
I get this compiler error:
Operator '==' cannot be applied to operands of type 'T' and 'T'
That's because there's no definition for == in T (that's actually a generic
type)?
Thanks,
Giulio
p.s. I solved using array.Equals(item), of course...