小编use*_*310的帖子

可以将一个特定的"从int转换"作为C#中的类型约束吗?

我有一些看起来像这样的代码:

class A<T>
      where T : // what should I say here to make this work?
{
   void foo()
   {
      int x = 0;
      T y = (T)x; // this is a compile error without proper type restrictions
   }
}
Run Code Online (Sandbox Code Playgroud)

目的是使用A类型,如double,float,int.

c# generics type-constraints

7
推荐指数
1
解决办法
113
查看次数

标签 统计

c# ×1

generics ×1

type-constraints ×1