none
Single line if statement without else RRS feed

  • Question

  • in C# we can write single line if statement

    if(condition)
    {
    somevalue = value1
    }
    else
    {
    somevalue = value2
    }
    
    // we can re-write this
    somevalue == condition? value1:value2
    
    but what if i have only IF and no ELSE, can we write single line IF?
    
    if(condition)
    {
    somevalue = value1
    }

    Monday, October 22, 2012 7:36 PM

Answers

  • You don't.  The conditional operator cannot be used for a single `if` statement.

    The closest you could do would be to set the variable to itself in the else case:

    someValue = condition ? newValue : someValue;

    Generally speaking, if you're asking this question then chances are you should just be using a regular `if` statement.

    • Proposed as answer by JMCF125 Monday, October 22, 2012 7:44 PM
    • Marked as answer by lax4u Monday, October 22, 2012 8:16 PM
    Monday, October 22, 2012 7:40 PM
  • I think the right way to write it is

    somevalue = condition ? value1 : value2;

    but I don't use it a lot, so I'm not sure. And to the question, "if i have only IF and no ELSE, can we write single line IF?", I'm sure that no; but you can omit the curly braces.


    João Miguel

    • Proposed as answer by JMCF125 Monday, October 22, 2012 7:44 PM
    • Marked as answer by Jason Dot Wang Tuesday, October 30, 2012 8:56 AM
    Monday, October 22, 2012 7:42 PM
  • You can always write:

    if (condition) someValue = value1;

    That's perfectly legal syntax, and allows you to place everything on one line.

    Note that the conditional operator  (?:) in C# is not the same as if (x) { } else { } - there are much stricter rules on what goes on both sides of the colon in the conditional operator, in that both must be an expression that evaluates to a value, and the value on both sides must be the same type or have an implicit conversion from one type to the other...


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    Monday, October 22, 2012 7:42 PM
    Moderator
  • I see three possible situations:

    1)

    if(condition)
        somevalue = value1;
    else
        somevalue = value2;
        
    2)

    somevalue = value2;
    if(condition)
        somevalue = value1;

    3)
    somevalue == (condition? value1 : value2);


    • Proposed as answer by JMCF125 Monday, October 22, 2012 7:44 PM
    • Marked as answer by Jason Dot Wang Tuesday, October 30, 2012 8:56 AM
    Monday, October 22, 2012 7:42 PM

All replies

  • You don't.  The conditional operator cannot be used for a single `if` statement.

    The closest you could do would be to set the variable to itself in the else case:

    someValue = condition ? newValue : someValue;

    Generally speaking, if you're asking this question then chances are you should just be using a regular `if` statement.

    • Proposed as answer by JMCF125 Monday, October 22, 2012 7:44 PM
    • Marked as answer by lax4u Monday, October 22, 2012 8:16 PM
    Monday, October 22, 2012 7:40 PM
  • I think the right way to write it is

    somevalue = condition ? value1 : value2;

    but I don't use it a lot, so I'm not sure. And to the question, "if i have only IF and no ELSE, can we write single line IF?", I'm sure that no; but you can omit the curly braces.


    João Miguel

    • Proposed as answer by JMCF125 Monday, October 22, 2012 7:44 PM
    • Marked as answer by Jason Dot Wang Tuesday, October 30, 2012 8:56 AM
    Monday, October 22, 2012 7:42 PM
  • You can always write:

    if (condition) someValue = value1;

    That's perfectly legal syntax, and allows you to place everything on one line.

    Note that the conditional operator  (?:) in C# is not the same as if (x) { } else { } - there are much stricter rules on what goes on both sides of the colon in the conditional operator, in that both must be an expression that evaluates to a value, and the value on both sides must be the same type or have an implicit conversion from one type to the other...


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    Monday, October 22, 2012 7:42 PM
    Moderator
  • I see three possible situations:

    1)

    if(condition)
        somevalue = value1;
    else
        somevalue = value2;
        
    2)

    somevalue = value2;
    if(condition)
        somevalue = value1;

    3)
    somevalue == (condition? value1 : value2);


    • Proposed as answer by JMCF125 Monday, October 22, 2012 7:44 PM
    • Marked as answer by Jason Dot Wang Tuesday, October 30, 2012 8:56 AM
    Monday, October 22, 2012 7:42 PM
  • Sorry for the duplicate. Your replie wasn't there when I posted mine. At least I confirmed what I was thinking about the conditional operator.

    João Miguel

    Monday, October 22, 2012 7:43 PM
  • Thanks All
    • Edited by lax4u Monday, October 22, 2012 8:15 PM
    Monday, October 22, 2012 8:15 PM
  • https://docs.microsoft.com/fr-fr/dotnet/csharp/language-reference/operators/null-conditional-operator 

    int i = GetNullableInt() ?? default(int);




    • Edited by cbolavie Wednesday, December 13, 2017 12:55 PM
    Wednesday, December 13, 2017 12:54 PM
  • Doing this should be abit of an overkill:

    (condition? () => somevalue = value1 : (Action)delegate{}).Invoke();

    For your case, this should suffice:

    somevalue = condition? value1 : somevalue;

    I use it like this:

    (SomeNameIsEmpty? () => throw new Exception("Name is empty") : (Action)delegate{}).Invoke();

    Microsoft Visual Studio 2019, .Net 4.7.2



    • Edited by Eu Jeen Tuesday, December 3, 2019 7:38 AM
    Tuesday, December 3, 2019 7:36 AM