c# - Passing generic argument to overloaded method -


is possible pass generic argument overloaded method?

class shop {   void createbutton<t>(t itemtobuy)   {     var button = createbuttonimpl();     button.onclick.addlistener(() => buy(itemtobuy));   }    void buy(c c) {}   void buy(d d) {} }  class c {} class d {} 

in above code, there error t cannot converted c. there way fix it?

the method shouldn't generic in first place, because can support 2 specific types, rather type (making generic in general sense of word).

just have 2 overloads caller, 1 accepts c , 1 accepts d.

there no way solve problem in way allows static type checking. if really have it, best can check type of parameter (i.e. through is) , call each overload directly. can let compiler write code using dynamic, still lose of static type safety, take notable performance hit.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -