Note: I've seen this question asked sometimes before (a, b, c), but neither of these was in C#, nor helpful.
Assume I'm using the ? :
ternary operator like this (to do nothing when false
is the case):
r==5? r=0 : <nothing> ;
I'm getting an error. Putting something there will obviously solve the problem. How can I still keep the other side empty without making some random empty function?
You can't. The whole point of the conditional ?: operator is that it evaluates an expression. You can't even just use:
Foo() ? Bar() : Baz();
... because that isn't a statement. You have to do something with the result... just like when you access a property, for example.
If you want to only execute a piece of code when a specific condition is met, the ?: operator isn't what you want - you want an if
statement:
if (foo)
{
bar();
}
It's as simple as that. Don't try to twist the conditional operator into something it's not meant to be.
See more on this question at Stackoverflow