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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:51:57+00:00 2026-06-08T18:51:57+00:00

The logic if-else statement, in the below class, is as short as c# will

  • 0

The logic if-else statement, in the below class, is as short as c# will allow, or can this statement be even shorter?

The logic structure ?: here on MSDN is a short-cut in some circumstances but not for the below?

class Stats
{
    public int Total = 0;
    public int Missed = 0;
    public int Correct = 0;
    public int Accuracy = 0;

    void Update(Boolean correctKey)
    {
        //correctKey ? Correct++ : Missed++; //incorrect syntax for this situation as the ? operator is used to set a value
        if (correctKey==true) Correct++;  else  Missed++; //shortest c# syntax?
    }
}
  • 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-08T18:52:00+00:00Added an answer on June 8, 2026 at 6:52 pm

    The conditional operator is intended to be used with assignments, not as a generalized replacement for the if/else statements. This is because it produces a result (it is an expression), and that result is then assigned to something else.

    For example, consider the following statement:

    var a = x != null ? x : y;
    

    This sets the value of variable a to object x if it is not null, or if it is, object y. The goal here is readability. Some programmers find it easier to read a statement with extremely common and straightforward logic like that one in a single line, rather than spread out into if/else blocks.

    (This pattern is essentially anachronistic in C# now that we have the null-coalescing operator, but it serves as a good instructional case.)

    Conversely, what you’re trying to express here doesn’t really work with the conditional operator since it doesn’t have a result and is not performing an assignment.

    In this case, I would say that an if/else block is your best choice. Note that shorter code does not necessarily produce faster code. In fact, in more cases than not, the compiler or the JITer will produce equivalent code that has identical performance. Don’t (ab)use the conditional operator to try and make your code faster, only use it when it makes your code more clear and readable. If readability or expressiveness requires breaking something up into multiple lines, then there’s nothing wrong with doing exactly that.

    As other examples show, you can in fact mash the if/else blocks into a single line:

    if (correctKey) Correct++; else Missed++;
    

    But this also doesn’t make the code run any faster, it just crams it all together. Like run-on sentences, this should be used judiciously where the meaning is unmistakable.

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

Sidebar

Related Questions

simple logic here, I don't know how I can separate the value from a
My logic is if( !this.draginited() ) // a drag-disabled element shouldn't get pass here,
I need the following logic. If array contains value , return it else return
why won't this work? function login(){ if(window.XMLHttpRequest){ ajax=new XMLHttpRequest(); }else if(window.ActiveXObject){ ajax=new ActiveXObject(Microsoft.XMLHTTP); }
What is the logic behind google's pagination behaviour? My paginator goes something like this:
so here the logic for 1%=| in the TLabel and for one | we
Is there a clean way to combine the SQL below into 1 statement? The
I have a logical statement that says If everyone plays the game, we will
I'm struggling to understand why my if statement below always results in false. I
after commit of response as here redirect statement should give exception but it is

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.