Nested dispose is required?

3

I have the following hypothetical code.

using (var QDR = new DAL.teste())
{
   //codigos
   using (var QDR1 = new DAL.teste1())
   {
      //codigos
      using (var QDR2 = new DAL.teste2())
      {
            // códigos
      }
   }
 }

Is the above code valid, but would it be necessary to put all the objects inside the using ? since in the first using it would not release all the variables in memory?

    
asked by anonymous 27.06.2016 / 15:39

1 answer

3

Dispose() does not release variables, it frees resources.

Objects in memory (not to be confused with features) are only released by the garbage collector at a time when it is up to them to decide. Variables are "freed" when the object containing it is released, or if it is local at the time it closes its scope. Learn more about scope and time and life .

Each using will call the Dispose() only of the resource that it created, nothing more. So it is necessary, but you can do it simpler.

Depending on the use it is possible to simplify this code putting everything in a single block, in this case just do:

using (var QDR = new DAL.teste())
using (var QDR1 = new DAL.teste1())
using (var QDR2 = new DAL.teste2()) {
    // etc
}

I changed the class ( MemoryStream ) just to make it easier and I had IL generated just to get a sense of what it does:

.class public auto ansi beforefieldinit C
    extends [mscorlib]System.Object
{
    // Methods
    .method public hidebysig 
        instance void M () cil managed 
    {
        // Method begins at RVA 0x2050
        // Code size 61 (0x3d)
        .maxstack 1
        .locals init (
            [0] class [mscorlib]System.IO.MemoryStream,
            [1] class [mscorlib]System.IO.MemoryStream,
            [2] class [mscorlib]System.IO.MemoryStream
        )

        IL_0000: nop
        IL_0001: newobj instance void [mscorlib]System.IO.MemoryStream::.ctor()
        IL_0006: stloc.0
        IL_0007: newobj instance void [mscorlib]System.IO.MemoryStream::.ctor()
        IL_000c: stloc.1
        IL_000d: newobj instance void [mscorlib]System.IO.MemoryStream::.ctor()
        IL_0012: stloc.2
        IL_0013: nop
        IL_0014: nop
        IL_0015: leave.s IL_0022
        IL_0017: ldloc.2
        IL_0018: brfalse.s IL_0021
        IL_001a: ldloc.2
        IL_001b: callvirt instance void [mscorlib]System.IDisposable::Dispose()
        IL_0020: nop
        IL_0021: endfinally
        IL_0022: leave.s IL_002f
        IL_0024: ldloc.1
        IL_0025: brfalse.s IL_002e
        IL_0027: ldloc.1
        IL_0028: callvirt instance void [mscorlib]System.IDisposable::Dispose()
        IL_002d: nop
        IL_002e: endfinally
        IL_002f: leave.s IL_003c
        IL_0031: ldloc.0
        IL_0032: brfalse.s IL_003b
        IL_0034: ldloc.0
        IL_0035: callvirt instance void [mscorlib]System.IDisposable::Dispose()
        IL_003a: nop
        IL_003b: endfinally
        IL_003c: ret

        Try IL_0013-IL_0017 Finally IL_0017-IL_0022
        Try IL_000d-IL_0024 Finally IL_0024-IL_002f
        Try IL_0007-IL_0031 Finally IL_0031-IL_003c
    } // end of method C::M

    .method public hidebysig specialname rtspecialname 
        instance void .ctor () cil managed 
    {
        // Method begins at RVA 0x20c4
        // Code size 8 (0x8)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: call instance void [mscorlib]System.Object::.ctor()
        IL_0006: nop
        IL_0007: ret
    } // end of method C::.ctor

} // end of class C
    
27.06.2016 / 15:46