作者jamod (jasper)
看板C_Sharp
标题[问题] 用Where来限制泛型的问题...
时间Tue Sep 8 18:31:48 2015
如题,小弟想要用类似以下做法来做一个工具:
public void StartDelay<T>(Action _action,T _count)where T:int,float
{
if(_count.GetType() == typeof(float))
{
//do something
}
else if(_count.GetType() == typeof(int))
{
//do something
}
}
但是他会跳一个错误,似乎是int和float在where的约束上面会有冲突,
虽然我可以在函式内加一些防呆防止使用者用错,但感觉不是很好=""=
希望能由compiler直接警告使用者,而不是执行到一半才跳错
想请问有没有办法使用where来完成,或是类似的方式?
非常感谢~~
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 60.249.2.10
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1441708310.A.6E5.html
1F:→ iterator: 你应该要提供 void StartDelay(Action, int) 09/08 18:37
2F:→ iterator: void StartDelay(Action, float) 09/08 18:38
对齁@@ 脑袋真的没转过来 非常感谢!
※ 编辑: jamod (36.233.20.131), 09/08/2015 20:18:24