Reputation: 71
I'm looking to use a single line if statement with the ?
operator to call return on a void method.
This is the full form statement:
if (failPaths.Count == paths.Count) {
return;
}
I'm aware that I could do something like if (failPaths.Count == paths.Count) return;
but I was just interested in seeing how it would be done with the ?
operator.
Upvotes: 5
Views: 10121
Reputation: 64943
In C# there's no way to do what you want to achieve.
Ternary operator requires that both true
and false
values evaluates to the type of the left side of the assignment expression (or method/property return value).
Since there's no explicit way of returning/setting void
, there's no way to have this in C#.
Upvotes: 3
Reputation: 4927
I think you're trying to say the "? : " operator.
Unfortunately, there's no way to do that, cause the compiler expect "else" condition.
(condition)?true:else
Upvotes: 0
Reputation: 26886
This can't be done using conditional operator mentioned by you as ?
operator.
In fact, conditional operator just evaluates condition and return either first or secod from two expressions - so it can be considered as expression itself.
But in your example if
statement is used not for expression evaluation, but for control flow.
Upvotes: 11