2016-11-13 27 views
-3

我得到这个编译器错误:这是什么编译器错误与泛型

The type 'Generics.Widget' cannot be used as type parameter 'T' in the generic type or method 'Generics.MyGenerics.Maximum<T>(T, T, T)' . There is no implicit reference conversion from 'Generics.Widget' to 'System.IComparable<Generics.Widget>'

见附件屏幕截图中的代码编译器错误尝试使用这个类。

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 

    namespace Generics { 
     // A class that implements IComparable 
     class Widget : System.IComparable { 
      private String name; 

      public Widget(String name) { 
       this.name = name; 
      } 

      int System.IComparable.CompareTo(object obj) { 
       return name.CompareTo(((Widget)obj).name); 
      } 
     } 
    } 

Screen Shot of compiler error

+0

约束显示实际/相关代码。 – user2864740

+1

我期望降低薪水,但我认为人们太苛刻了......这不是我们见过的最糟糕的问题。是的,OP可以发布最大值方法,但错误指的是类和相关的代码显示... –

+0

@GeorgeVovos我预计没有降薪。我总是高估了SO居民。 – nicomp

回答

1

您需要实现

public interface IComparable<in T> 


class Widget : System.IComparable<Widget> 
{ 
    public int CompareTo(Widget other) 
    { 
     throw new NotImplementedException(); 
    }  
} 

您还没有表现出你的最大方法IComparable的

的通用版本,但显然它有对T

where T : IComparable<Widget>