C# generic method with integer constraint refuses to cast from integer to the generic type

前端 未结 3 823
时光说笑
时光说笑 2021-01-15 04:37

If I have a generic method that is constrained to be type \'int\' then surely I should be able to cast an integer to the generic T type. For example...

    p         


        
3条回答
  •  不知归路
    2021-01-15 05:01

    Are you sure its compiling?

    Here, it gives following error:

    error CS0701: 'int' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter.

提交回复
热议问题