I have a generic collection MyCollection<T> that I’ve made, and everything works fine except this new function Apply that I’m adding:
class MyCollection<T> {
T value;
public MyCollection(T starter) { value = starter; }
public MyCollection<S> Apply<T, S>(Func<T, S> function) {
return new MyCollection<S>(function(value)); // error in function(value)
}
}
This gives me an error I’ve never seen before:
Argument 1: cannot convert from 'T' to 'T [C:\folder\code.cs (line number)]'
What are the two T types? What’s wrong with the conversion I’m attempting?
You problem is that the type parameter T in
is not the same type parameter as T in
so your
functiontakes another type than the type ofvalueif you change
to
your code will compile