-3

I have this code in my application:

protected async override void OnAppearing()
{
    base.OnAppearing();
    Subscribe();
    cardSetPhrases = App.DB.GetPhrasesForCardSet(cardSetId);
    var num = 1;
    foreach (var x in cardSetPhrases) { x.Row = num++; };
    await CreateListSectionAsync();
}

The IDE is giving me a message here:

my

kara
  • 3,205
  • 4
  • 20
  • 34
Alan2
  • 23,493
  • 79
  • 256
  • 450

5 Answers5

3

foreach loop is closed with }, then next statement is started (from compiler point of view). And immediately closed with your ;

Single ; is called empty statement and useful when compiler require to have a statement (inside loop or if/else etc) but you don't need any real action there.

From docs:

The empty statement consists of a single semicolon. It does nothing and can be used in places where a statement is required but no action needs to be performed.

That's why you got warning about "empty statement" and not about "extra semicolon". Because you put empty statement where it's not required and not used.

Dmitry
  • 16,110
  • 4
  • 61
  • 73
1

If you write your foreach-statement like this, maybe it gets clearer, why you don't need the additional semicolon at the end:

foreach (var x in cardSetPhrases)
{
    x.Row = num++;
} // <-- no semicolon, because the { } make a complete block of statements.
user11909
  • 1,235
  • 11
  • 27
0

A Semicolon is not needed after a foreach loop, so there is nothing to terminate for it. Thats why it is reduntant.

TheBlueOne
  • 486
  • 5
  • 13
0

Because you do not need to close a foreach loop with a semi-colon (it's not their syntax), so it's thinking you just made an empty statement with just ; and emptiness preceeding (since the foreach is closed by } (curly brackets)).

Also, foreach loops, even if they can be written in a single line, are usually formatted in multi-line, as such :

foreach (var x in cardSetPhrases) 
{
    x.Row = num++;
}

If you want to learn more about how they function, Check these out:

dotnetperls, docs.microsoft, geeksforgeeks

Antry
  • 448
  • 3
  • 9
0

Remove the semi-colon after the closing block bracket. And also, if you want to write one statement in a for, foreach, if etc. statement you can remove the brackets like this:

foreach (var x in cardSetPhrases) x.Row = num++;
Gergő Gutyina
  • 129
  • 1
  • 12