Performance / Scope question of usage of using

Forgive me if this is answered elsewhere. I haven't figured out the nuances of effective searches.

I've been programming for 40 years but newbie when it comes to C#. I keep seeing youtube videos people promoting the use of the using statement for creation connections to databases, stating that it automatically does the cleanup at the completion and avoids having a large number of sessions open, etc....

I created a class to deal with my I-O to an Oracle database using Dapper - to read the data I use the following code.

        public List<T> ReadData(DynamicParameters dynamicParameters)
    {
        using var conn = OpenConnection();
        QueryString = QueryString.Replace("==", FieldNames);
        var results = conn.Query<T>(QueryString, dynamicParameters).AsList();
        return results;
    }

My question is - how is the above different than:

        public List<T> ReadData(DynamicParameters dynamicParameters)
    {
        var conn = OpenConnection();
        QueryString = QueryString.Replace("==", FieldNames);
        var results = conn.Query<T>(QueryString, dynamicParameters).AsList();
        return results;
    }

Doesn't the completion of the method essentially do the same thing? Or does the usage of the using statement do more?



Read more here: https://stackoverflow.com/questions/68462269/performance-scope-question-of-usage-of-using

Content Attribution

This content was originally published by Rocky Marquiss at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: