Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5962399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:06:00+00:00 2026-05-22T19:06:00+00:00

I have seen someone write below kind of Func<> pattern. And I am trying

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:06:01+00:00Added an answer on May 22, 2026 at 7:06 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you ever seen someone try to implement the MVP pattern with ASP.NET Web
I really was trying to avoid asking this question. I have seen quite a
I have seen examples of printing from a windows application but I have not
I have seen lots of questions recently about WPF... What is it? What does
I have seen Solutions created in Visual Studio 2008 cannot be opened in Visual
I have seen simple example Ajax source codes in many online tutorials. What I
I have seen the references to VistaDB over the years and with tools like
I have seen this problem arise in many different circumstances and would like to
I have seen a few mentions of this idiom (including on SO ): //
I have seen the official demos on lwjgl.org but I would like to see

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.