Practical advantage of generics vs interfaces

前端 未结 8 1536
梦毁少年i
梦毁少年i 2020-11-29 02:35

What would be a practical advantage of using generics vs interfaces in this case:

void MyMethod(IFoo f) 
{
}

void MyMethod(T f) : where T : IFoo
{
         


        
8条回答
  •  伪装坚强ぢ
    2020-11-29 03:18

    The interface method will supply you an f of type IFoo, whereas the generic version will supply you a type T with the constraint that T has to implement IFoo.

    The second method would allow you to have some kind of lookup depending on T, as you have a concrete type to work with.

提交回复
热议问题