Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Method in Non-Generic Interface [duplicate]

What is the difference between having a "Generic Interface" and "Generic method in non generic interface" ? Is there any advantage of one over the other ?

interface IMyInterface
{
     void MyMethod<T>(T param) where T : class;
}

and

interface IMyInterface<T>  where T : class
{
   void MyMethod(T param)
}
like image 272
Abi Avatar asked May 06 '26 03:05

Abi


1 Answers

Yes, the non-generic method can't force the generic method to have specific type parameters. The generic interface can.

In your IMyInterface<T>.MyMethod, T can only be the exact same type used by the IMyInterface<T>. In IMyInterface.MyMethod, T can be anything (as long as it obeys the type constraint T : class).

like image 189
Patrick Hofman Avatar answered May 07 '26 18:05

Patrick Hofman