Archive

Archive for April, 2009

Why using the using keyword instead of try/finally

April 20, 2009 1 comment

I was confused that they both accomplish the same mission, then why using ?!

Maybe with using I’ll write less lines of code..
Maybe with using I’ll write more maintainable code..

Well, That’s true. Let’s take an example of disposing a SqlConnection object,

This is the way you do it with try finally, the compiler won’t get out of the method blocks to return the value unless it executes the finally block to ensure closing the connection.

        private static string GetString2(string connectionString)
        {
            SqlConnection connection = null;
            SqlCommand cmd = null;

            try
            {
                connection = new SqlConnection(connectionString);
                cmd = connection.CreateCommand();
                cmd.CommandText = "SELECT StringColumn FROM SomeTable";
                connection.Open();
                return Convert.ToString(cmd.ExecuteScalar());
            }
            finally
            {
                connection.Close();
            }
        }

Well, with using the compiler won’t get out of the using block unless it executes the Dispose() method on the resource object if it serves the IDisposable interface.

        private static string GetString(string connectionString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = "SELECT StringColumn FROM SomeTable";
                connection.Open();
                return Convert.ToString(cmd.ExecuteScalar());
            }
        }

Have you noticed any differences? This is the Code Metrics,
Code Metrics

So, how can I benefit from the using keyword to automatically free my resources when my object gets disposed?
Simply implement the IDisposable interface in your class, and use your class inside the using block.

    class Program
    {
        static void Main()
        {
            using (MyClass myClass = new MyClass())
            {

            }

            throw new InvalidOperationException();
        }
    }

    public class MyClass : IDisposable
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }

And off course the NotImplementedException will fire before the InvalidOperationException.

Hope this makes sense, Thanks!

Categories: .NET Development

Top Twitter Tools

If you haven’t seen them yet, I just liked this post so much.
http://zebolsen.com/top-twitter-tools-exposed-explained-at-lightning-speed/

Categories: .NET Development

Hello again!

Hello again folks!!

I’m sorry the last post was months ago, but lets just start over.

Off course I’m welcome to wordpress 😛 I know things are messy I’ve just moved from blogspot, imported my posts and comments just to find how easy and hard too to get my stuff here in the right way!.

I can’t hide my love to wordpress with all the themes, widgets, etc. I must say I liked it so much, except that I’ll have to edit most of the posts to make them look nice with the current theme.

I’ll be writing a series of posts on What do I learn in DashSoft, so don’t miss it 😉

Categories: .NET Development