开发者

C# List<T> Sort with IComparer argument gives compilation error

Can someone explain to me how this is supposed to work? I followed an MSDN example I found at http://msdn.microsoft.com/en-us/library/234b841s.aspx .

I have made my own CustomObject and have made a Comparer for it.

Here is the CustomObjectComparer class:

public class CustomObjectComparer : System.Collections.Generic.IComparer<CustomObject>
{
    public int Compare(CustomObject co1, CustomObject co2)
开发者_如何学Go    {
         //Impementation Omitted
    }

}

Then when I have a List<CustomObject> and try to do the following I get compile errors.

List<CustomObject> list = new List<CustomObject>();
CustomObjectComparer comparer = new CustomObjectComparer();
list.Sort(comparer);

Errors: Argument 1: cannot convert from 'CustomObjectComparer' to 'System.Collections.Generic.IComparer<CustomObject>'

Isn't CustomObjectComparer a System.Collections.Generic.IComparer?


It looks your list contains CustomObjectComparers, not CustomObjects.

You can either pass a comparer that can compare CustomObjectComparers, or (more likely) change the list to a List<CustomObject>.

EDIT: This would happend if you have two types named CustomObject, or if you also have a different error.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜