Hopefully I've described it correctly. I have a 'generic method' which looks like the below. It accepts a list of any Icomparable/Iequatable type and returns a class 'compareResult' shown below containing lists of matched/unmatched items.
public partial class Comparers
{
public class compareResult<T>
{
public List<T> unchangedItems;
public List<T> changedItems;
public List<T> leftOrphans;
public List<T> rightOrphans;
}
public s开发者_C百科tatic compareResult<T> stepCompare<T>(List<T> leftList, List<T> rightList, bool confirmUniqueIDs = true) where T : IEquatable<T>, IComparable
{
...
I now try to pass in a list of 'LicencedCustomer' which is defined as below, and implements the CompareTo and Equals methods to implement the IComparable/IEquatable interfaces.
public class LicencedCustomer : IEquatable<LicencedCustomer>, IComparable<LicencedCustomer>
{
public string LMAA_CODE {get; set;}
...
Now I try to pass two lists of customers per below:
Comparers.compareResult<LicencedCustomer> result = new Comparers.compareResult<LicencedCustomer>();
result = Comparers.stepCompare(leftList, rightList);
But it says "Error 1 The type 'MFTests.LicencedCustomer' cannot be used as type parameter 'T' in the generic type or method 'MF.Comparers.stepCompare(System.Collections.Generic.List, System.Collections.Generic.List, bool)'. There is no implicit reference conversion from 'MFTests.LicencedCustomer' to 'System.IComparable'...
I thought I had implemented IComparable though it refers to conversion which I don't really understand. Sorry for the long explanation, I tried to keep it as brief as possible.
Any thoughts on what I'm doing wrong?
The generic method does not include the generic type identifier, T
.
where T : IEquatable<T>, IComparable
should be
where T : IEquatable<T>, IComparable<T>
精彩评论