interface as argument or generic method with where - what is the difference?

前端 未结 4 1506
灰色年华
灰色年华 2021-01-17 11:46

Is any difference between :

public void Method1(class1 c, T obj) where T:Imyinterface

And

public void Method2(clas         


        
4条回答
  •  悲&欢浪女
    2021-01-17 12:47

    There is no big difference for void methods.

    public void Method1(class1 c, T obj) where T : Imyinterface
    

    equals to

    public void Method2(class1 c, Imyinterface obj, Type t)
    

    where t must be Imyinterface.

    So if you need to pass some Type to your method, and you need to apply some constraints to that Type at compile-time, use the generic method.

提交回复
热议问题