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
class MyCollection<T>
is not the same type parameter as T in
Apply<T, S>
so your function
takes another type than the type of value
if you change
Apply<T, S>
to
Apply<S>
your code will compile
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With