Difference between generic argument constrained to an interface and just using the interface

后端 未结 7 966
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 17:40

What is the difference between this:

void MyMethod(IMyInterface value)
{
    //...
}

and this:

void MyMethod(T val         


        
7条回答
  •  情书的邮戳
    2020-12-17 18:24

    Yet another difference for generic methods in general (though not for your example) is that if one has a method like T MungeThing(T it) where T:IMungeable and class Fnord implements IMungeable, then code will be able to say: Fnord thing1, thing2; ... thing1 = MungeThing(thing2); and the compiler will know that MungeThing will return a Fnord rather than some arbitrary implementation of IMungable.

提交回复
热议问题