Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Single line if with void return

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.

like image 841
Beepo Avatar asked Dec 10 '22 18:12

Beepo


1 Answers

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.

like image 113
Andrey Korneyev Avatar answered Dec 18 '22 10:12

Andrey Korneyev