C # volume using keyword

I understand that whenever I create an instance of a class that implements IDisposable, I must use the keyword usingto make sure that it is properly disposed of.

Same:

using (SecureString s = new SecureString())
{

}

This is easy to understand, I can use it s, but I want it in these brackets, but as soon as I leave these brackets, I can no longer refer to s. Scope is easy to see.

But I do not understand how this works when you use usingwithout enclosed brackets.

private void Function()
{
    // Some code here

    using (SecureString s = new SecureString())

    // more code here
}

You are not required to use parentheses at all ... so ... how do I know where I can use an object and where it will be deleted if there are no parentheses to go with the usingkeyword?

+4
11

#, , .

if:

if (someBool)
    DoSomething();
else
    DoSomethingElse();

, :

if (someBool)
{
    // do lots of things
}
else
    DoSomethingElse();

, , , { }.

using , :

using (var stream = new NetworkStream(...))
using (var sslStream = new SslStream(stream))
using (var reader = new StreamReader(sslStream))
{
    reader.WriteLine(...);
}

:

using (var stream = new NetworkStream(...))
{
    using (var sslStream = new SslStream(stream))
    {
        using (var reader = new StreamReader(sslStream))
        {
            reader.WriteLine(...);
        }
    }
}

, .

+8

A using , using - , if.

using (SecureString s = new SecureString())
    s.Foo(); // Works
s.Foo(); // out of scope

, if/using, , .

+6

ony using.

, . .

+3

.

using(var s = new SecureString())
    s.Whatever();
s.Whatever() // ERROR! s is out of scope

, if :

if(x == y)
    x = 4;
+2

, using, if foreach, , - ( ). , . .

if (someCondition)
    Console.WriteLine("someCondition is true!");
Console.WriteLine("I run regardless");
// same as
if (someCondition)
{
    Console.WriteLine("someCondition is true!");
}

using (var s = new SecureString())
    Console.WriteLine(s); // this works!
//Console.WriteLine(s); // s is no longer in scope, this won't compile
+2

, using, :

using(var s = new SecureString()) s.Whatever();

, , , , , . , , .

+2

:

using (SecureString s = new SecureString())
    // This statement is in using scope.

// This is not.
+1

,

using (SecureString s = new SecureString())
    // statement here

, .

+1

, . "if" "for". :

for (int i = 0; i < 10; i++)
    Console.WriteLine(i.ToString());

if (str == "hello")
    Console.WriteLine("hello back!");

using (SecureString s = new SecureString())
    Console.WriteLine("Here we have s");

Console.WriteLine("but here it out of scope already");
+1

using, , for, while, if, else , "" .

, , , ( ) . using if, , " ". , , , .

0

, .

  static private void Function()
  {
     Console.WriteLine("before");

     using (SecureString s = new SecureString())
        s.Clear();

     Console.WriteLine("after");
  }

Using IL Dissassembler, the function described above parses the code below. You can see that the statement is usingimplemented with the help try-finallythat completes the rest of the statement (which can be a block defined by curly braces, but in this case it is only a semicolon). The object you are using is located in a block finally. Then, outside finally, the code continues the next statement from the source.

.method private hidebysig static void  Function() cil managed
{
  // Code size       56 (0x38)
  .maxstack  2
  .locals init ([0] class [mscorlib]System.Security.SecureString s,
           [1] bool CS$4$0000)
  IL_0000:  nop
  IL_0001:  ldstr      "before"
  IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000b:  nop
  IL_000c:  newobj     instance void [mscorlib]System.Security.SecureString::.ctor()
  IL_0011:  stloc.0
  .try
  {
    IL_0012:  ldloc.0
    IL_0013:  callvirt   instance void [mscorlib]System.Security.SecureString::Clear()
    IL_0018:  nop
    IL_0019:  leave.s    IL_002b
  }  // end .try
  finally
  {
    IL_001b:  ldloc.0
    IL_001c:  ldnull
    IL_001d:  ceq
    IL_001f:  stloc.1
    IL_0020:  ldloc.1
    IL_0021:  brtrue.s   IL_002a
    IL_0023:  ldloc.0
    IL_0024:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()
    IL_0029:  nop
    IL_002a:  endfinally
  }  // end handler
  IL_002b:  nop
  IL_002c:  ldstr      "after"
  IL_0031:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0036:  nop
  IL_0037:  ret
} // end of method Program::Function
0
source

All Articles