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 CustomObjectComparer
s, not CustomObject
s.
You can either pass a comparer that can compare CustomObjectComparer
s, 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.
精彩评论