Reputation: 1409
I have seen someone write below kind of Func<> pattern. And I am trying to experiment with Funcs and Lambdas to get the concepts right.
so ExperimentalSelect returns a Func (with 2 args and bool return value).
But I fail to understand that how all of the 3 return statements are valid (1 at a time).
public static Func<BinaryTreeNode<int>, int, bool> nodeSelector = (x, y) =>
{
return x.Value > y;
};
public static Func<int, int, bool> intSelector = (x, y) =>
{
return x > y;
};
public static Func<BinaryTreeNode<int>, int, bool> ExperimentalSelect (int number)
{
return nodeSelector; // seems straightforward, i agree this should work
// how does this work ? intSelector is different type Func<>
// and it is executing it here, thereby returning the bool type result
// and not Func<> type as the return type of this method should
return (x, y) => intSelector(number, number);
// and how does this work ? what is node here ?
// and SomeMethod returns bool and not Func<> type
return (node, x) => SomeMethod(node, number, true);
}
private static bool SomeMethod(BinaryTreeNode<int> node, int someNumber,
bool doSomething)
{
return node.Value < someNumber;
}
EDIT:
If an ext. method expects a func
IEnumerable<int> selected = tree.TakeWhile(ExperimentalSelect);
How does SomeMethod work here ?, it is not a Func<> !
What does this syntax mean: (node, x) => SomeMethod(node, number, true);
Where is the node or x in picture here ?
Upvotes: 4
Views: 4105
Reputation: 174299
Your comments above the returns are not correct.
intSelector
and SomeMethod
are not executed there. They will only be executed, when the return value of ExperimentalSelect
is executed.
return (x, y) => intSelector(number, number);
This defines an anonymous method with two parameters. One of type BinaryTreeNode<int>
and one of type int and a return value of bool, because that's what's the return type of ExperimentalSelect
. The body of this anonymous method is the call to intSelector
which is executed only, when the anonymous method itself is executed. This means, the body of this anonymous method can be anything you wish. It can even be multiple statements:
return (x, y) => {
var temp;
temp = y;
y = x;
x = temp;
return intSelector(number, y);
}
The anonymous method then is returned and not the result of executing it.
Your return statement is equivalent to the following:
Func<BinaryTreeNode<int>, int, bool> result =
(x, y) => intSelector(number, number);
return result;
You can verify this yourself with your debugger. Add a break point inside intSelector
and step over the return
statement. You will see, the breakpoint will not be hit.
One important point is the following:
Func
can be seen as a pointer to a function.
Func<BinaryTreeNode<int>, int, bool> result =
(x, y) => SomeMethod(x, number, true);
This will create an anonymous method and result
will point to that anonymous method.
However, have a look at the following code:
Func<BinaryTreeNode<int>, int, bool, bool> result = SomeMethod;
In this case, result will directly point to SomeMethod
. No anonymous method is created here. Note the difference in the type of result
. Because the first code only executes SomeMethod
in the body of the anonymous method, the type of result
doesn't need to match the signature of SomeMethod
. But in the second code, you directly assign SomeMethod
to result
and thus the type of result
must match the signature of SomeMethod
.
More:
Look at the following code:
public static Func<BinaryTreeNode<int>, int, bool> ExperimentalSelect (int number)
{
return (x, y) => intSelector(number, number);
}
Func<BinaryTreeNode<int>, int, bool> result = ExperimentalSelect(10);
Console.WriteLine(result(30, 20)); // writes false
It will print false
, although 30 is bigger than 20. Why?
The reason is, that your anonymous method has x
and y
as input parameters, but they are not used anywhere in its body. Instead, you pass number
as both parameters to intSelector
. The value of number
is 10, and 10 isn't greater than 10.
The correct way to write this code would be like this:
public static Func<BinaryTreeNode<int>, int, bool> ExperimentalSelect ()
{
return (x, y) => intSelector(x, y);
}
Func<BinaryTreeNode<int>, int, bool> result = ExperimentalSelect();
Console.WriteLine(result(30, 20)); // writes true
As you can see, I now pass x
and y
to intSelector
. I also removed the parameter number
from ExperimentalSelect
, because it is not used anywhere.
Upvotes: 4
Reputation: 12381
You seem to think that in this case
return (x, y) => intSelector(number, number);
what gets returned is the intSelector(number, number)
part. But in reality the whole thing after return
keyword is a return value. You can rewrite it like this:
return (BinaryTreeNode<int> x, int y) => intSelector(number,number);
which is roughly equivalent to returning an anonymous delegate with two parameters of type BinaryTreeNode<int>
and int
which returns the value of type bool. So it's your Func<BinaryTreeNode<int>, int, bool>
right there.
Basically, the part to the left of =>
describes arguments and the part to the right is the method body, and the return type of the whole thing is the return type of the body.
Take a look at Lambda Expressions article by Eric White, it'll make sense to you.
Upvotes: 2
Reputation: 42333
Ask yourself - are these two lines of code also correct:
Func<BinaryTreeNode<int>, int, bool> a =
(x, y) => intSelector(number, number);
Func<BinaryTreeNode<int>, int, bool> b =
(node, x) => SomeMethod(node, number, true);
The answer - yes. As it is for the return statements in your code.
I think perhaps you're either slightly misunderstanding the syntax or perhaps not understanding exactly what a Func
is (apologies if either is not the case).
Upvotes: 2