Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Func<> and extension methods question

Tags:

c#

.net

lambda

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);
  1. How does SomeMethod work here ?, it is not a Func<> !

  2. What does this syntax mean: (node, x) => SomeMethod(node, number, true);

Where is the node or x in picture here ?

like image 788
Munish Goyal Avatar asked Feb 24 '23 01:02

Munish Goyal


1 Answers

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.

like image 197
Daniel Hilgarth Avatar answered Feb 26 '23 16:02

Daniel Hilgarth