Skip to main content
added 108 characters in body
Source Link

The Dispose method is called immediately after the code in the using statement. To confirm this we can take a peek at the IL that is generated from a using statement:

The following simple example:

 class Program { static void Main() { using (new TestClass()) { Console.WriteLine("Do work"); } } } class TestClass : IDisposable { public void Dispose() { Console.WriteLine("disposed"); } } 

Produces the following IL.

Program.Main: IL_0000: nop IL_0001: newobj UserQuery+TestClass..ctor IL_0006: stloc.0 IL_0007: nop IL_0008: ldstr "Do work" IL_000D: call System.Console.WriteLine IL_0012: nop IL_0009IL_0013: nop IL_0014: leave.s IL_0016IL_0021 IL_000BIL_0016: ldloc.0 IL_000CIL_0017: brfalse.s IL_0015IL_0020 IL_000EIL_0019: ldloc.0 IL_000FIL_001A: callvirt System.IDisposable.Dispose IL_0014IL_001F: nop IL_0015IL_0020: endfinally IL_0016IL_0021: ret Program..ctor: IL_0000: ldarg.0 IL_0001: call System.Object..ctor IL_0006: nop IL_0007: ret TestClass.Dispose: IL_0000: nop IL_0001: ldstr "disposed" IL_0006: call System.Console.WriteLine IL_000B: nop IL_000C: ret TestClass..ctor: IL_0000: ldarg.0 IL_0001: call System.Object..ctor IL_0006: nop IL_0007: ret  

Here we can see the dispose method being called immediately after the code in the using statement.

The Dispose method is called immediately after the code in the using statement. To confirm this we can take a peek at the IL that is generated from a using statement:

The following simple example:

 class Program { static void Main() { using (new TestClass()) { Console.WriteLine("Do work"); } } } class TestClass : IDisposable { public void Dispose() { Console.WriteLine("disposed"); } } 

Produces the following IL.

Program.Main: IL_0000: nop IL_0001: newobj UserQuery+TestClass..ctor IL_0006: stloc.0 IL_0007: nop IL_0008: nop IL_0009: leave.s IL_0016 IL_000B: ldloc.0 IL_000C: brfalse.s IL_0015 IL_000E: ldloc.0 IL_000F: callvirt System.IDisposable.Dispose IL_0014: nop IL_0015: endfinally IL_0016: ret Program..ctor: IL_0000: ldarg.0 IL_0001: call System.Object..ctor IL_0006: nop IL_0007: ret TestClass.Dispose: IL_0000: nop IL_0001: ldstr "disposed" IL_0006: call System.Console.WriteLine IL_000B: nop IL_000C: ret TestClass..ctor: IL_0000: ldarg.0 IL_0001: call System.Object..ctor IL_0006: nop IL_0007: ret 

Here we can see the dispose method being called immediately after the code in the using statement.

The Dispose method is called immediately after the code in the using statement. To confirm this we can take a peek at the IL that is generated from a using statement:

The following simple example:

 class Program { static void Main() { using (new TestClass()) { Console.WriteLine("Do work"); } } } class TestClass : IDisposable { public void Dispose() { Console.WriteLine("disposed"); } } 

Produces the following IL.

Program.Main: IL_0000: nop IL_0001: newobj UserQuery+TestClass..ctor IL_0006: stloc.0 IL_0007: nop IL_0008: ldstr "Do work" IL_000D: call System.Console.WriteLine IL_0012: nop IL_0013: nop IL_0014: leave.s IL_0021 IL_0016: ldloc.0 IL_0017: brfalse.s IL_0020 IL_0019: ldloc.0 IL_001A: callvirt System.IDisposable.Dispose IL_001F: nop IL_0020: endfinally IL_0021: ret Program..ctor: IL_0000: ldarg.0 IL_0001: call System.Object..ctor IL_0006: nop IL_0007: ret TestClass.Dispose: IL_0000: nop IL_0001: ldstr "disposed" IL_0006: call System.Console.WriteLine IL_000B: nop IL_000C: ret TestClass..ctor: IL_0000: ldarg.0 IL_0001: call System.Object..ctor IL_0006: nop IL_0007: ret  

Here we can see the dispose method being called immediately after the code in the using statement.

Source Link

The Dispose method is called immediately after the code in the using statement. To confirm this we can take a peek at the IL that is generated from a using statement:

The following simple example:

 class Program { static void Main() { using (new TestClass()) { Console.WriteLine("Do work"); } } } class TestClass : IDisposable { public void Dispose() { Console.WriteLine("disposed"); } } 

Produces the following IL.

Program.Main: IL_0000: nop IL_0001: newobj UserQuery+TestClass..ctor IL_0006: stloc.0 IL_0007: nop IL_0008: nop IL_0009: leave.s IL_0016 IL_000B: ldloc.0 IL_000C: brfalse.s IL_0015 IL_000E: ldloc.0 IL_000F: callvirt System.IDisposable.Dispose IL_0014: nop IL_0015: endfinally IL_0016: ret Program..ctor: IL_0000: ldarg.0 IL_0001: call System.Object..ctor IL_0006: nop IL_0007: ret TestClass.Dispose: IL_0000: nop IL_0001: ldstr "disposed" IL_0006: call System.Console.WriteLine IL_000B: nop IL_000C: ret TestClass..ctor: IL_0000: ldarg.0 IL_0001: call System.Object..ctor IL_0006: nop IL_0007: ret 

Here we can see the dispose method being called immediately after the code in the using statement.