Thoughts from a .Net developer
Friday, September 19, 2003
 
This week I realized that I never got around to writing about the using statement.

The link above describes how to use it an what it does pretty good, so I will just talk about the MSIL. Below I have a simple function that uses the using statement and then the same function without the using statement. As Microsoft explains in the documentation (link above), the block of code that uses the using statement ends up being a try...finally block. This of course is safer code to write and probably a good practice to use. If you look at the differences between the two function's IL code you will notice:


  1. The using statement writes safer, better and more IL code

  2. Without the using statement you get only what you ask for, which will be less code - but not better code



C# Code

public void UsingStatement()
{
using(SqlConnection conn = new SqlConnection("connectionstring"))
{
SqlCommand cmd = new SqlCommand("insert statement", conn);
conn.Open();
cmd.ExecuteNonQuery();
}
}
public void WithoutUsingStatement()
{
SqlConnection conn = new SqlConnection("connection");
SqlCommand cmd = new SqlCommand("insert statement", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}


The IL is as follows:

.method public hidebysig instance void UsingStatement() cil managed
{
// Code size 49 (0x31)
.maxstack 3
.locals init ([0] class [System.Data]System.Data.SqlClient.SqlConnection conn,
[1] class [System.Data]System.Data.SqlClient.SqlCommand cmd)
IL_0000: ldstr "connectionstring"
IL_0005: newobj instance void [System.Data]System.Data.SqlClient.SqlConnection::.ctor(string)
IL_000a: stloc.0
.try
{

IL_000b: ldstr "insert statement"
IL_0010: ldloc.0
IL_0011: newobj instance void [System.Data]System.Data.SqlClient.SqlCommand::.ctor(string,
class [System.Data]System.Data.SqlClient.SqlConnection)
IL_0016: stloc.1
IL_0017: ldloc.0
IL_0018: callvirt instance void [System.Data]System.Data.SqlClient.SqlConnection::Open()
IL_001d: ldloc.1
IL_001e: callvirt instance int32 [System.Data]System.Data.SqlClient.SqlCommand::ExecuteNonQuery()
IL_0023: pop
IL_0024: leave.s IL_0030
} // end .try
finally
{

IL_0026: ldloc.0
IL_0027: brfalse.s IL_002f
IL_0029: ldloc.0
IL_002a: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_002f: endfinally
} // end handler
IL_0030: ret
} // end of method usingstatement::UsingStatement

.method public hidebysig instance void WithoutUsingStatement() cil managed
{
// Code size 43 (0x2b)
.maxstack 3
.locals init ([0] class [System.Data]System.Data.SqlClient.SqlConnection conn,
[1] class [System.Data]System.Data.SqlClient.SqlCommand cmd)
IL_0000: ldstr "connection"
IL_0005: newobj instance void [System.Data]System.Data.SqlClient.SqlConnection::.ctor(string)
IL_000a: stloc.0
IL_000b: ldstr "insert statement"
IL_0010: ldloc.0
IL_0011: newobj instance void [System.Data]System.Data.SqlClient.SqlCommand::.ctor(string,
class [System.Data]System.Data.SqlClient.SqlConnection)
IL_0016: stloc.1
IL_0017: ldloc.0
IL_0018: callvirt instance void [System.Data]System.Data.SqlClient.SqlConnection::Open()
IL_001d: ldloc.1
IL_001e: callvirt instance int32 [System.Data]System.Data.SqlClient.SqlCommand::ExecuteNonQuery()
IL_0023: pop
IL_0024: ldloc.0
IL_0025: callvirt instance void [System.Data]System.Data.SqlClient.SqlConnection::Close()
IL_002a: ret
} // end of method usingstatement::WithoutUsingStatement


Overall, useing the using statement in my code should create better code (safer from crashing) with less typing.
Saturday, September 06, 2003
 
Today I went to Best Buy and bought a Wireless access point. Since wireless isn't completely secure yet, I have been holding off on doing this. Reasons why I now am trying it out:

1. I got a wireless adapter for my laptop a few weeks ago
2. I have been trying to scan for other wireless networks in my apartment building (with netstumbler) and have not picked any up - which leads me to believe my elderly neighbors aren't going to hack my network.
3. I can always turn it off - especially when I am not using it!
4. If I want to know how to use it, the best way to learn is to use it
5. If I want to learn how to secure a wireless network, I need to know how to hack it -- which is made easier if I have my own to hack.


Monday, September 01, 2003
 
Got the laptop rebuilt with all the latest and greatest. Went ahead and backed up the system state already (ready for it to crash). It did crash before I even finished setting up win2k3 server this morning, had to run a check disk and copy over the important config files...spent at least an hour an a half on this.

After taking a long walk I came back and finished installing all of the software (or at least a good start).

Tomorrow I am planning on writing about something I have been looking at: the using statement.

This statement seems to be a little known useful C# specific shortcut (to writing good code - with less typing).

Powered by Blogger