C# Empty Statement

后端 未结 13 2252
情深已故
情深已故 2020-11-29 12:34

The C# language specification defines the empty-statement grammar production, which allows me to do something like this:

static void Main(string[] a         


        
相关标签:
13条回答
  • 2020-11-29 12:54

    So some genius could write for(;;) instead of while(true). Like for an interview test.

    0 讨论(0)
  • 2020-11-29 12:56

    C# inherits a lot from the C family, where you can do things like

    for (i = 0; i < n; v[i++] = 1);
    

    or

    while (testSomeCondition());
    

    That is, run a loop with an empty body where the "meat" is inside the brackets.

    While one can debate about their merits and dangers, these are pretty common programming idioms in the C programming world, so this backward compatibility makes it easier for C/C++/Java programmers to learn C#.

    0 讨论(0)
  • 2020-11-29 12:56

    This is an assumption but I would assume it goes to the base grammar for the compiler. Relating it to set theory the "empty set" is inside every single set. That the ; really is to define a set of lexical operations which always must define the ; which also would accept the base case which is empty statement.

    0 讨论(0)
  • 2020-11-29 12:56
    if (b1)
       if (b2) else;
    else
       //code
    

    withoud this ; this will be painful. but this is not best way to write ifs also. but acceptable.

    0 讨论(0)
  • 2020-11-29 12:58

    For what it's worth, a good rule of thumb is that you should have stepped through in the debugger every line of code you write, at least once for each possible execution path

    ref: Possible mistaken empty statement

    0 讨论(0)
  • 2020-11-29 13:03

    Good practice or not, some people use this:

    while (!SomeCondition)
        ; // <-- body of the while loop is an empty statement
    
    0 讨论(0)
提交回复
热议问题