Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About list sorting and delegates & lambda expressions Func stuff

Tags:

c#

List<bool> test = new List<bool>();
test.Sort(new Func<bool, bool, int>((b1, b2) => 1));

What Am I missing?

Error 2 Argument 1: cannot convert from 'System.Func' to 'System.Collections.Generic.IComparer'

Error 1 The best overloaded method match for 'System.Collections.Generic.List.Sort(System.Collections.Generic.IComparer)' has some invalid arguments

When I have

private int func(bool b1, bool b2)
{
    return 1;
}

private void something()
{
    List<bool> test = new List<bool>();
    test.Sort(func);
}

it works fine. Are they not the same thing?

like image 655
haxxoromer Avatar asked Jul 23 '12 20:07

haxxoromer


1 Answers

Func is the wrong delegate type. You can use either of these:

test.Sort((b1, b2) => 1);
test.Sort(new Comparison<bool>((b1, b2) => 1));
like image 155
usr Avatar answered Nov 15 '22 00:11

usr