1

I have the following C# code:

        if (client.Action == "show")
        {
            result = "s";
        }
        else
        {
            result = answersCorrect ? "t" : "f";
        }

Is there a way I can remove the if and else and use two levels of ? and :

4 Answers4

6

Sure, you can:

var result = client.Action == "show" ? "s" : (answersCorrect ? "t" : "f");

A good question to ask yourself, though, is whether this approach is more readable.

MrDustpan
  • 5,508
  • 6
  • 34
  • 39
2

use

var result = client.Action == "show" ? "s" : (answersCorrect ? "t" : "f");
Sajad Karuthedath
  • 14,987
  • 4
  • 32
  • 49
1
 result = client.Action == "show" ?  "s" : answersCorrect ? "t" : "f"
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

Try this:

result=(client.Action == "show")?"s":((answersCorrect) ? "t" : "f");
Vinod
  • 4,672
  • 4
  • 19
  • 26