传递微观的< t>进行比较
我有以下代码:
public class Foo<T>
{
private IComparable<T> a { get; set; }
public int foo(IComparable<T> b)
{
return a.CompareTo(b); // compile error : Argument type 'System.IComparable<T>' is not assignable to parameter type 'T?'
}
}
参数类型'system.icomable'不能分配给参数类型't?'
如何避免此错误?
I have a following code:
public class Foo<T>
{
private IComparable<T> a { get; set; }
public int foo(IComparable<T> b)
{
return a.CompareTo(b); // compile error : Argument type 'System.IComparable<T>' is not assignable to parameter type 'T?'
}
}
Argument type 'System.IComparable' is not assignable to parameter type 'T?'
How can I avoid this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在课程级别上添加通用约束,以确保
t
实现iComable&lt; t&gt;
。然后用JustT
在属性和参数类型中替换iComparable&lt; t&gt;
。Add a generic constraint at the class level to ensure that
T
implementsIComparable<T>
. Then replaceIComparable<T>
in the property and parameter type with justT
.