2
private void MainForm_Load(object sender, EventArgs e)
{
     Func<int, bool> f = funn;
     var list = new List<int>();
     list.Add(32);
     list.Add(1);
     list.Add(2);
     list.Add(3);
     MessageBox.Show(list.Where(f).First().ToString());//I give only f
}
private bool funn(int k)
{
        return k == 12;
}

See the Where clause argument

private void MainForm_Load(object sender, EventArgs e)
{
     Func<int, bool> f = funn;
     var list = new List<int>();
     list.Add(32);
     list.Add(1);
     list.Add(2);
     list.Add(3);
     MessageBox.Show(list.Where(i=>f(i)).First().ToString());//Now I give f with i
}
private bool funn(int k)
{
        return k == 12;
}

Well, What's the difference between those king of calling filter method?

levi
  • 3,451
  • 6
  • 50
  • 86
  • The second one utilizes a lambda to call your method, so I believe the `Where` clause ends up calling 2 methods (the lambda, and `f`). Whether the compiler optimizes that fact away, I wouldn't know. The tangible difference is that if you don't have update 2 for visual studio 2012, you cannot set a breakpoint in `funn` and have it hit. – Matthew Jun 19 '13 at 13:30
  • Related: [Are there any benefits to using a C# method group if available?](http://stackoverflow.com/q/3841990/861716). – Gert Arnold Jun 19 '13 at 20:30

1 Answers1

2

There's no semantic difference for that specific example because:

Where(method)

will expect method to have a signature bool method(T object) where T is the type of the enumerable, and it will just call method() for each object in the sequence.

Where( item => method(item) )

does exactly the same thing, because it passes each object in the sequence to method().

However, if you need to (for example) use some property within item to do the test, you then must use the explicit lambda.

For example, imagine if item had a Text property that you wanted to pass to method() you'd have to do:

Where( item => method(item.Text) )

In this case, method() would have the signature bool method(string).

More Details

The reason that you can just use the name of a method instead of a lambda delegate is because of "Method Group Conversions".

This is covered in detail in section 6.6 of the C# language spec. To simplify, what it means is that if a method has the same signature as a delegate, it can be automatically converted to such a delegate type.

From the spec:

An implicit conversion (§6.1) exists from a method group (§7.1) to a compatible delegate type. Given a delegate type D and an expression E that is classified as a method group, an implicit conversion exists from E to D if E contains at least one method that is applicable in its normal form (§7.5.3.1) to an argument list constructed by use of the parameter types and modifiers of D, as described in the following.

(And then it goes on in much more detail, which I won't post here!)

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • 1
    That's not quite true; they handle conversions differently. – SLaks Jun 19 '13 at 13:35
  • @SLaks Even for that specific example? What conversion would it handle differently for the specific example in the OP? I'm missing it... Indeed, `Resharper` suggests converting the second form into the first. – Matthew Watson Jun 19 '13 at 13:37