0

Say i want to do something like this

T1 GenericMethod<T1>(T2 arg)
    where T1 : GenericClass<T2>
{
    ...
}

seems it isn't possible, i have to write like this

T1 GenericMethod<T1, T2>(T2 arg)
    where T1 : GenericClass<T2>
{
    ...
}

So the question is, can i somehow make first sample work? (passing only one type parameter, the other must be automaticly calculated related to class'es parameter)

Yeah, and one more question, does boxing happen when i use type parameter with where TValue : ISomething, if i would call method from value (struct) type passed as type parameter? Or it is only a hint for compiler to use certain methods? I need to call method from value type, but i don't want boxing to be appeared

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Can you not just return a `GenericClass`? – Sayse Jul 27 '15 at 08:40
  • 1
    Closed as duplicate. In a nutshell: You need to specify either *all* generic parameters, or *all* must be deducable by the compiler. There is no in-between. Oh, and only one question per question, please! :-) – Heinzi Jul 27 '15 at 08:42
  • I can return GenericClass, but it means that i'll have to cast result to more specific type. Basically question is why i have to declare (not even pass) T2 manually, i can make mistake passing wrong T2 type, but if compiler will check that GenericClass is abstract (which i forgot to mention), so in this situation there is only one T2 defined for given T1, no need to define it manually – Ilya Mihalin Jul 27 '15 at 10:04
  • I tried with interface instead of abstract class - result is same – Ilya Mihalin Jul 27 '15 at 10:14

0 Answers0