|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionI was at work today, and someone asked me what happens when you use the TestsI decided I was going to run through a couple of tests: Test #1I wanted to see what the generated MSIL code looks like when I use the Test #2I wanted to see if I could write code without using the Test #1Here is my sample code: [STAThread]
private static void Main(string[] args)
{
using (Bitmap bitmap1 = new Bitmap(100, 100))
{
Console.WriteLine("Width: {0}, Height: {1}", bitmap1.Width, bitmap1.Height);
}
Console.ReadLine();
}
As you can see... nothing special in the code. Create a new What does this look like when we build the app, then decompile it into MSIL? Check it out: .method private hidebysig static void Main(string[] args) cil managed
{
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor()
.entrypoint
.maxstack 4
.locals init (
[0] [System.Drawing]System.Drawing.Bitmap bitmap1)
L_0000: ldc.i4.s 100
L_0002: ldc.i4.s 100
L_0004: newobj instance void
[System.Drawing]System.Drawing.Bitmap::.ctor(int32, int32)
L_0009: stloc.0
L_000a: ldstr "Width: {0}, Height: {1}"
L_000f: ldloc.0
L_0010: callvirt instance int32 [System.Drawing]System.Drawing.Image::get_Width()
L_0015: box int32
L_001a: ldloc.0
L_001b: callvirt instance int32 [System.Drawing]System.Drawing.Image::get_Height()
L_0020: box int32
L_0025: call void [mscorlib]System.Console::WriteLine(string, object, object)
L_002a: leave.s L_0036
L_002c: ldloc.0
L_002d: brfalse.s L_0035
L_002f: ldloc.0
L_0030: callvirt instance void [mscorlib]System.IDisposable::Dispose()
L_0035: endfinally
L_0036: call string [mscorlib]System.Console::ReadLine()
L_003b: pop
L_003c: ret
.try L_000a to L_002c finally handler L_002c to L_0036
}
Test #1 ResultsSo the results from test #1 are interesting. The Test #2Based on the MSIL code that resulted from Test #1, I decided to write the same code using [STAThread]
private static void Main(string[] args)
{
Bitmap bitmap1 = new Bitmap(100, 100);
try
{
Console.WriteLine("Width: {0}, Height: {1}", bitmap1.Width, bitmap1.Height);
}
finally
{
if (bitmap1 != null)
{
bitmap1.Dispose();
}
}
Console.ReadLine();
}
And this is what it looked like in MSIL: .method private hidebysig static void Main(string[] args) cil managed
{
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor()
.entrypoint
.maxstack 4
.locals init (
[0] [System.Drawing]System.Drawing.Bitmap bitmap1)
L_0000: ldc.i4.s 100
L_0002: ldc.i4.s 100
L_0004: newobj instance void
[System.Drawing]System.Drawing.Bitmap::.ctor(int32, int32)
L_0009: stloc.0
L_000a: ldstr "Width: {0}, Height: {1}"
L_000f: ldloc.0
L_0010: callvirt instance int32 [System.Drawing]System.Drawing.Image::get_Width()
L_0015: box int32
L_001a: ldloc.0
L_001b: callvirt instance int32 [System.Drawing]System.Drawing.Image::get_Height()
L_0020: box int32
L_0025: call void [mscorlib]System.Console::WriteLine(string, object, object)
L_002a: leave.s L_0036
L_002c: ldloc.0
L_002d: brfalse.s L_0035
L_002f: ldloc.0
L_0030: callvirt instance void [System.Drawing]System.Drawing.Image::Dispose()
L_0035: endfinally
L_0036: call string [mscorlib]System.Console::ReadLine()
L_003b: pop
L_003c: ret
.try L_000a to L_002c finally handler L_002c to L_0036
}
Test #2 ResultsIt is almost exactly like the MSIL generated from the [STAThread]
private static void Main(string[] args)
{
using (Bitmap bitmap1 = new Bitmap(100, 100))
{
Console.WriteLine("Width: {0}, Height: {1}", bitmap1.Width, bitmap1.Height);
}
Console.ReadLine();
}
This looks exactly like our original code!!! ConclusionThis was really a fun experiment that revealed a lot about the way .NET works behind the scenes. I hope you guys enjoy this as much as I did. History
|
||||||||||||||||||||||