开发者

C# generic method rejecting a type even though it implements required interface

开发者 https://www.devze.com 2023-03-03 21:27 出处:网络
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 con

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消