C # "using" statement following try statement, can there be a bracket in this case?

  using System.Net;       // (See Chapter 16)
   ...
  string s = null;
  using (WebClient wc = new WebClient()) // why there is no brackets after this using statement
      try { s = wc.DownloadString ("http://www.albahari.com/nutshell/");  }
      catch (WebException ex)
      {
        if (ex.Status == WebExceptionStatus.Timeout)

          Console.WriteLine ("Timeout");
        else
          throw;     // Can't handle other sorts of WebException, so rethrow
      }

The above code is a copy from C # in a nutshell, I don’t understand why {} is missing after using the instruction, is the typo in the book (unlikely) or just not needed? Because the syntax is that use should be performed by a block of code inside {}.

I expect this code to be:

  using System.Net;       // (See Chapter 16)
   ...
  string s = null;
  using (WebClient wc = new WebClient()) // why there is no brackets after this using statement
  {
      try { s = wc.DownloadString ("http://www.albahari.com/nutshell/");  }
      catch (WebException ex)
      {
        if (ex.Status == WebExceptionStatus.Timeout)

          Console.WriteLine ("Timeout");
        else
          throw;     // Can't handle other sorts of WebException, so rethrow
      }
  }
+6
source share
3 answers

If you look at the grammar of an operator usingin C # Specification , you will see that using operators follows (or rather, their body consists of) "embedded_statements".

using_statement
    : 'using' '(' resource_acquisition ')' embedded_statement
    ;

:

embedded_statement
    : block
    | empty_statement
    | expression_statement
    | selection_statement
    | iteration_statement
    | jump_statement
    | try_statement
    | checked_statement
    | unchecked_statement
    | lock_statement
    | using_statement
    | yield_statement
    | embedded_statement_unsafe
    ;

, , . using (...) , embedded_statement. , , , .

+10

{} , - , . try.

if ( ):

if(x == 0) return; // No {} the next statement is affected by the if

{} , , .

+1

, Adrian's. :

, ?

, . NOP IL, Optimization . , :

public void CurlyBracesMethod(){
    {
        int r = 1;
        r+=2;
    }
    if(true)
        return;
}

IL, [ Optimization ]:

  .locals init (int32 V_0,
           bool V_1)
  IL_0000:  nop
  IL_0001:  nop
  IL_0002:  ldc.i4.1
  IL_0003:  stloc.0
  IL_0004:  ldloc.0
  IL_0005:  ldc.i4.2
  IL_0006:  add
  IL_0007:  stloc.0
  IL_0008:  nop
  IL_0009:  ldc.i4.1
  IL_000a:  stloc.1
  IL_000b:  br.s       IL_000d
  IL_000d:  ret

:

public void NonCurlyBracesMethod(){
    int r = 1;
    r+=2;
    if(true)
        return;
}

IL :

  IL_0000:  nop
  IL_0001:  ldc.i4.1
  IL_0002:  stloc.0
  IL_0003:  ldloc.0
  IL_0004:  ldc.i4.2
  IL_0005:  add
  IL_0006:  stloc.0
  IL_0007:  ldc.i4.1
  IL_0008:  stloc.1
  IL_0009:  br.s       IL_000b
  IL_000b:  ret

, , .

, . :

for(int i = 1;i<=n;i++){
       if(i%2==0){
         add+=1;
         flg=true;
    }
}

, add+=1 , , . flg=true . . , Curly Braces, , CurlyBracesMethod. r , . , , .

0

All Articles