Cannot implicitly convert type 'T' to 'Int'

后端 未结 6 1806
轻奢々
轻奢々 2020-12-16 14:38

When trying to call this function in my code i get the error in the title. Also Operator \'+=\' cannot be applied to the operands of type \'int\' and \'T\'

p         


        
6条回答
  •  既然无缘
    2020-12-16 15:03

    It knows not how to add your T to a numeric since it doesnt know what the type of T is going to be.

    t += Convert.ToInt32(value);
    

    But since you are adding int to int and returning int then why not just ditch the generic parameter and make it

    public int Change(Stats type, int value)  
    

    and if you want different behaviour for different types and really want the same method name, instead of testing the type just do:

     public int Change(Stats type, string value) 
     public int Change(Stats type, DateTime value)  
    

提交回复
热议问题