-1

Think about these 2 snippets:

Approach 1: use using statement

using(var connection = new SqlConnection())
using(var command = new SqlCommand(cmdText, connection)){
    try{
        connection.Open();
        using(var reader = command.ExecuteReader(
            CommandBehavior.CloseConnection | CommandBehavior.SingleResult){
            while(reader.Read())
                // read values
        }
    } catch (Exception ex) { 
        // log(ex);
    }
}

Approach 2: use try/finally

var connection = new SqlConnection();
var command = new SqlCommand(cmdText, connection);
SqlDataReader = null;
try{
    var reader = command.ExecuteReader(
        CommandBehavior.CloseConnection | CommandBehavior.SingleResult);
    while(reader.Read())
        // read values...
} catch (Exception ex) { 
    // log(ex);
} finally {
    command.Dispose();
    if (reader != null) {
        if (!reader.IsClosed)
            reader.Close();
        reader.Dispose();
    }
    if (connection.State != ConnectionState.Closed)
        connection.Close();
    connection.Dispose();
}

We all know that the using statements would be compiled to try/finally blocks. So is it correct to say: when the app get compiled, there would be 4 try blocks?

try { // for using SqlConnection

    try { // for using SqlCommand

        try { // my own try block

            try { // for using SqlDataReader

            } finally { 
                // dispose SqlDataReader
            }

        } catch { 
            // my own catch. can be used for log etc.
        }

    } finally {
        // dispose SqlCommand
    }

} finally {
    // dispose SqlConnection
}

And, if the answer is yes, wouldn't that be a performance issue? Generally, is there any, I mean any performance difference between using blocks and try/finally blocks?

UPDATE:

From comments, I've to say:

1- The important question is, having multiple try blocks inside each other: is there any performance issue?

2- I have to care of code, because I'm responsible to code, not to query. The query-side has its own developer which is doing his best. So, I have to do my best too. So, it's important to me to take care of milliseconds ;) Thanks in advance.

amiry jd
  • 27,021
  • 30
  • 116
  • 215
  • Are you really seeing any performance issues? I **can't** believe that the `using` code blocks would be causing performance issues, and would be more inclined to think your query is slow in executing. – mituw16 Apr 16 '15 at 12:58
  • 5
    4 `try/finally` - Yes. Performance problems - No. Why do you asking? Using `using` makes code clean. Use `using`. – Sinatr Apr 16 '15 at 12:58
  • If you're thinking about that level in performance, you shouldn't be using the managed code at all. – Sriram Sakthivel Apr 16 '15 at 12:59
  • Instead of thinking about the performance impact of `using` you should care about the performance of your query. Your query might run for hours but you're optimizing milliseconds by obfuscating the code. – Tim Schmelter Apr 16 '15 at 13:00
  • 2
    Obligatory [performance rant](http://ericlippert.com/2012/12/17/performance-rant/) – juharr Apr 16 '15 at 13:09

1 Answers1

1

Usually when you hear about try/catch is slow, it's all about exception handling. So if exception occurs then it might be slow. But just entering in try method is not something you should worry about. Especially in your case when you warp SQL query call.

If you want to know more about exceptions and performance in .NET you can find a lot of articles to read. For example: MSDN article or great CodeProject article.

And of course using is preferable way because it makes code much cleaner.

Pradeep
  • 3,258
  • 1
  • 23
  • 36
Aleksandr Ivanov
  • 2,778
  • 5
  • 27
  • 35
  • `Usually when you here about try/catch is slow, it's all about exception handling` good point. I like that +1 – amiry jd Apr 16 '15 at 13:13