1

If I am to write this piece of code, it works fine with the normal 'if-else' layout.

if(isOn)
{
    i = 10;
}
else
{
    i = 20;
}

Although I am unsure how to convert this using the ternary operator

        isOn = true ? i = 1 : i = 0;

Error: Type of conditional expression cannot be determined because there is no implicitly conversion between 'void' and 'void'.

EDIT: Answer = i = isOn ? 10 : 20;

Is it possible to do this with methods?

if(isOn)
{
    foo();
}
else
{
    bar();
}
3
  • Edited to add method question. Commented Aug 29, 2014 at 16:17
  • For your edit:, why do you want to do that with methods ? isn't your if clear enough ? also it depends what these methods are returning. If they are not returning anything void then I believe you can't use the ternary operators. Commented Aug 29, 2014 at 16:19
  • 3
    If you have a new question then ask a separate question, don't update the existing questions with a new question. Commented Aug 29, 2014 at 16:20

7 Answers 7

14

Please try the following. BTW, it only works for value assignments not method calls.

i = isOn ? 10 : 20;

Reference:

Sign up to request clarification or add additional context in comments.

7 Comments

Perfect thank you. I have edited the question, do you know if it is possible with methods?
@MarcC:- What are your methods returning? Since the answer depends on that
The methods are void.
Updated the answer. It's not possible for method calls.
@Habib very determined person can wrap void methods into lambda returning fake value for purely theoretical way of using ?: with void functions.
|
5

You may simply try this:

i = isOn? 10:20

The MSDN says:

The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes the result. If condition is false, second_expression is evaluated and becomes the result. Only one of the two expressions is evaluated.

EDIT:-

If you want to invoke void methods in a conditional operator, you can use delegates else it is not possible to use ternary operators for methods.

And if your methods are returning something then try like this:

i = isOn ? foo() : bar();    //assuming both methods return int

Comments

4

You're on the right track but a little off. i = isOn ? 10 : 20;

Here 10 will be assigned to i if isOn == true and 20 will be assigned to i if isOn == false

Comments

3

Here's an explanation that might help. The statement you're looking for is:

i = isOn ? 10 : 20;

And here's what that means:

(result) = (test) ? (value if test is true) : (value if test is false);

Comments

2

try the following

i = isOn ? 10 :20

Comments

2

Try the following:

i = isOn ? 10 : 20

Comments

-3

You need:

i = true ? 10 : 20;

where true is your condition.

3 Comments

isOn is the bool (condition) specified in OP's code
The OP has already posted the answer to this. i = isOn ? 10 : 20;
true part was just for the OP to replace it with the condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.