定义一个泛型类,其中T1约束为值类型,代码如下:
class DemoClass<T1,T2> where T1:struct
{
private T1 x;
private T2 y;
public DemoClass(T1 x,T2 y)
{
this.x = x;
this.y = y;
}
public void Show() { Console.WriteLine("{0},{1}", x, y); }
}主函数调用如下:
static void Main()
{
DemoClass<int, int> d = new DemoClass<int, int>(3, 5);
d.Show();
}以上程序没什么问题,但是我现在也要约束DemoClass<T1,T2>的T2也为值类型或者为T2增加约束,代码如何写呢?谢谢。
class DemoClass<T1,T2> where T1:struct
{
private T1 x;
private T2 y;
public DemoClass(T1 x,T2 y)
{
this.x = x;
this.y = y;
}
public void Show() { Console.WriteLine("{0},{1}", x, y); }
}主函数调用如下:
static void Main()
{
DemoClass<int, int> d = new DemoClass<int, int>(3, 5);
d.Show();
}以上程序没什么问题,但是我现在也要约束DemoClass<T1,T2>的T2也为值类型或者为T2增加约束,代码如何写呢?谢谢。
interface IMyInterface
{
} class Dictionary<TKey, TVal>
where TKey : IComparable, IEnumerable
where TVal : IMyInterface
{
public void Add(TKey key, TVal val)
{
}
}