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

  • Home
  • SEARCH
  • 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 4274264
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:49:25+00:00 2026-05-21T07:49:25+00:00

Background: I am trying to write a shortest possible lambda expression to find the

  • 0

Background: I am trying to write a shortest possible lambda expression to find the greatest of 3 numbers.
Of course ternery operator is capable of doing it.

Func<int, int, int, int> greatestNumber2 = (x, y, z) => (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z);

But my intention is to achieve a function such as the one below.

greatest (x, y, z) = if (greater (x, y)>z) then greater(x, y) else z;

I was able to do this in two ways.

            Func<int, int, int> greaterNumber = null;
            Func<int, int, int, int> greatestNumber = null;

            //Expression 1
            greaterNumber = (x, y) => x > y ? x : y;
            greatestNumber = (x, y, z) => greaterNumber(x, y) > z ? greaterNumber(x, y) : z;

            //Expression 2
            greatestNumber = (x, y, z) => {
                Func<int, int, int> greater = (i, j) => i > j ? i : j;
                return greater(x, y) > z ? greater(x, y) : z;
            };

In the Expression 2, I was able to somehow achieve the what I wanted i.e define the function to find the greater among two numbers from the same expression itself. However, it is a statment lambda.

Is there a way to write a single lined lambda to define and use the greater of 2 numbers from within the expression of greatest itself.

  • 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-21T07:49:26+00:00Added an answer on May 21, 2026 at 7:49 am

    How about building an array and using LINQ? Probably a bit easier to extend than working out all the logic paths with multiple inputs (untested):

    Func<int, int, int, int> f = (x, y, z) => new[] { x, y, z }.Max();
    

    EDIT: Just reread the question, it looks like you want to recurse the greatestNumber lambda, with just one lambda definition. I don’t think you could do that, as greatestNumber(x,y)‘s signature is different to greatestNumber(x,y,z). You could probably do something more generalised with a normal function that takes a params argument, but that doesn’t involve lambdas.

    EDIT 2: As @Anthony says, creating an array is probably overkill, although it is short and a one-liner. You could simplify your Expression 2 a bit:

    Func<int, int, int> greaterOf = (x, y) => x > y ? x : y;
    Func<int, int, int, int> greaterOf = (x, y, z) => greaterOf(x, greaterOf(y, z));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I'm trying to write an application in C++ that will run on Vista.
I'm new to C# (from a native C++ background) and I'm trying to write
I'm working to learn Scala--coming from a C++ background. I am trying to write
I'm trying to write some code for a slideshow background. Please not watch what
I'm trying to write a field expression for a Cell in my report where
Background: I'm trying to write a simple SoapExtension class to log inbound/outbound Soap messages
I'm trying to write a basic background keylogger... Keyboard scanCodes and States are converted
I am trying to write the letter 'D' in blue on a white background
Background: I'm trying to write a shell script using php that will automatically checkout
BACKGROUND I'm trying to write a function query query('type', 'parameters', 'bind_types') which I can

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.