c# - What is this compiler error with generics -
i getting compiler error:
the type
'generics.widget'cannot used type parameter 't' in generic type or method'generics.mygenerics.maximum<t>(t, t, t)'. there no implicit reference conversion'generics.widget''system.icomparable<generics.widget>'
see attached screen shot compiler error in code trying use class.
    using system;     using system.collections.generic;     using system.linq;     using system.text;     using system.threading.tasks;      namespace generics {         // class 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);             }          }     } 
you need implement generic version of icomparable
public interface icomparable<in t>   class widget : system.icomparable<widget> {     public int compareto(widget other)     {         throw new notimplementedexception();     }     } you haven't shown maximum method has constraint on t
where t : icomparable<widget> 
Comments
Post a Comment