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 7165321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:13:13+00:00 2026-05-28T14:13:13+00:00

When comparing to a minimum or maximum of two numbers/functions, does C# short-circuit if

  • 0

When comparing to a minimum or maximum of two numbers/functions, does C# short-circuit if the case is true for the first one and would imply truth for the second? Specific examples of these cases are

if(x < Math.Max(y, z()))

and

if(x > Math.Min(y, z()))

Since Math.Max(y, z()) will return a value at least as large as y, if x < y then there is no need to evaluate z(), which could take a while. Similar situation with Math.Min.

I realize that these could both be rewritten along the lines of

if(x < y || x < z())

in order to short-circuit, but I think it’s more clear what the comparison is without rewriting. Does this short-circuit?

  • 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-28T14:13:14+00:00Added an answer on May 28, 2026 at 2:13 pm

    As others have pointed out, the compiler knows nothing about the semantics of Min or Max that would allow it to break the rule that arguments are evaluated before the method is called.

    If you wanted to write your own, you could do so easily enough:

    static bool LazyLessThan(int x, int y, Func<int> z)
    {
        return x < y || x < z();
    }
    

    and then call it

    if (LazyLessThan(x, y, z))
    

    or

    if (LazyLessThan(x, y, ()=>z()))
    

    Or for that matter:

    static bool LazyRelation<T>(T x, T y, Func<T> z, Func<T, T, bool> relation)
    {
        return relation(x, y) || relation(x, z());
    }
    ...
    if (LazyRelation(x, y, ()=>z, (a,b)=> a < b))) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When comparing two strings in c# for equality, what is the difference between InvariantCulture
When comparing two objects (of the same type), it makes sense to have a
I'm comparing between two techniques to create partitioned tables in SQL 2005. Use partitioned
One obvious method for computing the minimum distance from a point to a 3D
Given a set of numbers, divide the numbers into two subsets such that difference
When I am comparing two files in Eclipse (using, for example, Compare With -->
Comparing floating point numbers(double, float) in .net directly for equality is not safe. A
I'm working on comparing two versions of a listview, for a settings form. I
I am comparing two table like this: SELECT * FROM tableA AS A INNER
When comparing MVC to ASP.NET Forms, which one offers a better design-time experience for

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.