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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:25:08+00:00 2026-06-04T23:25:08+00:00

This is what I want to do: if(ABoolean || (BBoolean && CBoolean)) { SomeButton.Enabled

  • 0

This is what I want to do:

if(ABoolean || (BBoolean && CBoolean))
{
    SomeButton.Enabled = true;
    AnotherButton.Enabled = true;
}
else 
{
    SomeButton.Enabled = false;
    AnotherButton.Enabled = false;
}

I can switch this to:

SomeButton.Enabled = (ABoolean || (BBoolean && CBoolean));
AnotherButton.Enabled = (ABoolean || (BBoolean && CBoolean));

For a much more succinct code. My question is, does the compiler optimize the assignment such that it will see that the boolean expression is the same and assign its value for the second button, or will it calculate the value each time.

Note: I understand this is a trivial example and that the speedup/slowdown will be minuscule to the point of inconsequentiality, but it will serve to give me a better understanding of compiler optimization.

Edit: Here is the reason why I thought the second option might be optimized:

class Program
{
    static bool ABoolean = true, BBoolean = true, CBoolean = false;
    static bool AEnable, BEnable;


    static void Main(string[] args)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        for (int i = 0; i < 1000000000; i++)
        {
            Operation1();
        }
        sw.Stop();
        Console.WriteLine(sw.ElapsedMilliseconds);

        Stopwatch sw1 = new Stopwatch();
        sw1.Start();
        for (int i = 0; i < 1000000000; i++)
        {
            Operation2();
        }
        sw1.Stop();
        Console.WriteLine(sw1.ElapsedMilliseconds);
        Console.Read();
    }

    static void Operation1()
    {
        if (ABoolean || (BBoolean && CBoolean))
        {
            AEnable = true;
            BEnable = true;
        }
        else
        {
            AEnable = false;
            BEnable = false;
        }
    }

    static void Operation2()
    {
        AEnable = (ABoolean || (BBoolean && CBoolean));
        BEnable = (ABoolean || (BBoolean && CBoolean));
    }
}

This resulted in an approximate ~8-9 second difference over the 1 billion operations (with the second option running faster). As I added more “Enable” booleans in however the second operation became slower.

  • 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-06-04T23:25:10+00:00Added an answer on June 4, 2026 at 11:25 pm

    No, I wouldn’t expect the compiler to optimize that. It’s possible that the JIT could optimize that (as it has more information) but I wouldn’t expect the C# compiler to.

    How could the compiler know whether SomeButton.Enabled will have some side-effect which could change the value of ABoolean, BBoolean or CBoolean?

    EDIT: Validation of this… let’s give the C# compiler the absolute most chance:

    class Test
    {
        static void Main()
        {
            Foo(true, false, true);
        }
    
        static void Foo(bool x, bool y, bool z)
        {
            A = x || (y && z);
            B = x || (y && z);
        }
    
        static bool A { get; set; }
        static bool B { get; set; }
    }
    

    Compile with:

    csc /o+ /debug- Test.cs
    

    Code for Foo via ILDASM:

    .method private hidebysig static void  Foo(bool x,
                                               bool y,
                                               bool z) cil managed
    {
      // Code size       37 (0x25)
      .maxstack  8
      IL_0000:  ldarg.0
      IL_0001:  brtrue.s   IL_000c
      IL_0003:  ldarg.1
      IL_0004:  brfalse.s  IL_0009
      IL_0006:  ldarg.2
      IL_0007:  br.s       IL_000d
      IL_0009:  ldc.i4.0
      IL_000a:  br.s       IL_000d
      IL_000c:  ldc.i4.1
      IL_000d:  call       void Test::set_A(bool)
      IL_0012:  ldarg.0
      IL_0013:  brtrue.s   IL_001e
      IL_0015:  ldarg.1
      IL_0016:  brfalse.s  IL_001b
      IL_0018:  ldarg.2
      IL_0019:  br.s       IL_001f
      IL_001b:  ldc.i4.0
      IL_001c:  br.s       IL_001f
      IL_001e:  ldc.i4.1
      IL_001f:  call       void Test::set_B(bool)
      IL_0024:  ret
    } // end of method Test::Foo
    

    As you can see, the expression really is evaluated in both cases.

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

Sidebar

Related Questions

I want this url http://www.youtube.com/watch?v=dgNgODPIO0w&feature=rec-HM-fresh+div to be transformed to: http://www.youtube.com/v/dgNgODPIO0w with php.
I want this to be on top... how can I do this and not
How do I get and print a char from a user? This want do
I want this: http://jqueryui.com/demos/dialog/#modal-message to happend when you click on ClickMe. how to do
I want this for some conditional compilation code that will run in all IE's
I only want this function to run if .toolbar li does not have the
My problem is this: I want to use mxml for visual element... i want
So I only want this JavaScript to be included in the response to the
I want to develop an Android typing based game. I would really want this
This is an image based project. In this I want to download image from

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.