0

Is there any way you can do something like this without using the usual If syntax?

foreach (var cell in ws.Cells[header_row, 1, header_row, ws.Dimension.End.Column])
{
    cell.Address == "SomeValue" ? break : continue;
}

Visual Studio doesn't seem to allow an inline If statement with this kind of results.

What am I doing wrong? Or is it actually not possible?

Thanks.

6
  • 8
    I don't know C#, but in most languages, an "inline if" (a ternary/conditional expression) doesn't allow statements. Ternarys are not simply a shorter version of an if. They're meant to evaluate to one of two values, and continue and break don't evaluate to anything. Commented Jan 29, 2020 at 13:55
  • 3
    turnary operator must return a value (or throw exception); neither break nor continue are values Commented Jan 29, 2020 at 13:57
  • When using the conditional operator you have to use either expressions or you can throw an exception. You cannot use control commands like break and continue. The operator isn't just short for an if-else. It's short for an if-else where both are assigning a value to the same variable. Commented Jan 29, 2020 at 13:57
  • Not sure whether this solution works in your case or not, you might want to give it a try though stackoverflow.com/a/53362143/2946329 Commented Jan 29, 2020 at 13:59
  • As other's have already noted you don't even need the continue since that's the default behavior when you reach the end of a loop. Note that you cannot have this break or continue logic in the middle of a loop as that would make the rest of the code that comes after it in the loop unreachable. Commented Jan 29, 2020 at 14:10

3 Answers 3

4
if(cell.Address == "SomeValue") break;

In your sample code, continue isn't needed.

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

Comments

3

You can use LINQ's TakeWhile to obtain the desired behaviour:

foreach (var cell in ws.Cells[header_row, 1, header_row, ws.Dimension.End.Column].TakeWhile(x => x.Address != "SomeValue"))
{

}

Comments

1

What your are doing with the ? isn't an inline if-statement, this operator is called the conditional operator (it's sometimes also called the ternary operator, but don't confuse it with the elvis operator).

As you can read from the Microsoft documentation, you don't use it to control flow of your application, but as a shorthand to conditionally assign something. As such, the ternary operator must either return a value or throw an exception.

Rewrite your code as such:

if (cell.Address == "SomeValue")
    break;

Notice how I omitted the continue; statement, as it is not needed in your case, as the continue statement jumps to the next iteration without completing this iteration (which I assume you don't want here)

Here is a short example of how to use the ternary:

// Instead of this:
if (foo)
    bar = "Foo";
else
    bar = "Not Foo";

// You can write this
bar = foo ? "Foo" : "Not Foo";

2 Comments

FYI it's official name is the conditional operator. Ternary refers to the fact that it's the only operator that takes 3 operands.
@Carcigenicate you're absolutely correct, did some further reading into the elvis operator and turns out that while syntactically similar they're quite different. I update my answer to respect it

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.