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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:25:26+00:00 2026-05-17T23:25:26+00:00

In C#, when setting a boolean variable’s value to false only when it is

  • 0

In C#, when setting a boolean variable’s value to false only when it is true, should I check if it is true before setting it or just set it?

Assuming the variable is true 50% of the time, checking it makes sense since a comparison is faster. If the variable is true most of the time, should I just skip the comparison?

Which is the better practice?

Method 1, checking first:

if (bVariable)
    bVariable = false;

or Method 2, Just setting it:

bVariable = false;

Under which conditions is method 2 preferred, if ever?

  • 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-17T23:25:26+00:00Added an answer on May 17, 2026 at 11:25 pm

    What makes you think that the comparison is faster? If the code you were writing was done in a C compiler, the IF statement would be split into at least two instructions — a comparison/branching instruction and a “set” instruction on a single bit on a single word.

    The “set” would compile to a single instruction. Your “optimization” could possibly make your program run slower, and would cause your program to be less readable. Just set the variable and don’t try to overthink little things.

    CPUs aren’t like databases. You don’t pay a high penalty for data modifications. You pay high penalties for making trips to main memory and for branching (if statements). Branches cost performance because pipelining CPUs actually start executing instructions after the branch before the branch instructions even make their decision! (I know, that statement is somewhat mind-blowing). But what that means is that the CPU has to spend resources “guessing” what the outcome of your IF statement is going to be. If it guesses wrong, it has to “throw away” the results of all instructions that it guessed would be executed after the branch and try again. It’s bad. It’s why branches are expensive.

    The moral of this particular story is not that you should never optimize, but that you should never optimize without completely understanding the implications of your optimization. In this case, had you gone with Option 1, you could possibly end up with a slower app which is less readable to boot.

    Actually, if you’re really interested in this sort of thing, you should definitely pick up a copy of Code Complete. It’s full of discussions about this sort of thing, and is brilliantly written.

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

Sidebar

Related Questions

No related questions found

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.