问个类型约束问题
c#吧
全部回复
仅看楼主
level 1
gmekil🌿 楼主
必须为实现了运算符 "+" 功能的类型约束该怎么写?
2019年01月28日 02点01分 1
level 7
这个跟类型约束有关系么。。。
2019年01月28日 08点01分 2
在C++里constraints(C++ 20)确实可以达到这个目的
2019年02月05日 04点02分
level 13
不行,可以考虑用抽象类实现此约束。
2019年01月29日 06点01分 4
level 4
impossible
2019年01月31日 02点01分 5
level 10
换个思路呗,何必抓着+不放。可以定义一个接口IAdd,然后where T : IAdd
2019年01月31日 02点01分 6
level 3
static dynamic MyAdd(dynamic a,dynamic b) {
return a + b;
}
static void Main(string[] args) {
WriteLine(MyAdd(1, 3));
WriteLine(MyAdd("gf1", "3fdgg"));
}
2019年02月04日 11点02分 7
现在看看自己昨天的回答,发现不符题意
2019年02月05日 10点02分
level 5
无非两种,数值型与字符串链接,看,自己,需求
2019年02月05日 04点02分 8
level 1
public class Foo<T> where T :class
{
public T Instance { get; private set; }
public static Foo<T> operator + (Foo<T> left , T right)
{
left.Instance = right;
return left;
}
}
2019年02月05日 05点02分 9
1