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 ?
Your comments above the returns are not correct.
intSelectorandSomeMethodare not executed there. They will only be executed, when the return value ofExperimentalSelectis executed.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 ofExperimentalSelect. The body of this anonymous method is the call tointSelectorwhich 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:The anonymous method then is returned and not the result of executing it.
Your return statement is equivalent to the following:
You can verify this yourself with your debugger. Add a break point inside
intSelectorand step over thereturnstatement. You will see, the breakpoint will not be hit.One important point is the following:
Funccan be seen as a pointer to a function.This will create an anonymous method and
resultwill point to that anonymous method.However, have a look at the following code:
In this case, result will directly point to
SomeMethod. No anonymous method is created here. Note the difference in the type ofresult. Because the first code only executesSomeMethodin the body of the anonymous method, the type ofresultdoesn’t need to match the signature ofSomeMethod. But in the second code, you directly assignSomeMethodtoresultand thus the type ofresultmust match the signature ofSomeMethod.More:
Look at the following code:
It will print
false, although 30 is bigger than 20. Why?The reason is, that your anonymous method has
xandyas input parameters, but they are not used anywhere in its body. Instead, you passnumberas both parameters tointSelector. The value ofnumberis 10, and 10 isn’t greater than 10.The correct way to write this code would be like this:
As you can see, I now pass
xandytointSelector. I also removed the parameternumberfromExperimentalSelect, because it is not used anywhere.