Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 891665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:55:33+00:00 2026-05-15T13:55:33+00:00

I really want to get this out of my head. Please see below code:

  • 0

I really want to get this out of my head. Please see below code:

using (DataTable resultTable = DBUtility.GetSingleDBTableResult(connectionString, "SELECT * FROM MyDBTable")) {
    List<string> resultsList = new List<string>();
    foreach (DataRow dataRow in resultTable.Rows) {
        resultsList.Add(dataRow[0].ToString());
    }
    return resultsList;
}

Is the datatable disposed? Can someone explain how this is translated to a try/catch/finally block? MSDN states that if an exception occurred, the Dispose method will still be called but what about the return statement?

Or should i just use below code:

List<string> resultsList = new List<string>();
using (DataTable resultTable = DBUtility.GetSingleDBTableResult(connectionString, "SELECT * FROM MyDBTable")) {
    foreach (DataRow dataRow in resultTable.Rows) {
        resultsList.Add(dataRow[0].ToString());
    }
}
return resultsList;

Probably, the second one should be used but I just need enlightenment :). Please explain. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:55:34+00:00Added an answer on May 15, 2026 at 1:55 pm

    using statement is just syntactic sugar and it gets translated into try/finally block. Starting with your code, here’s how the C# compiler will translate the using block into try/finally block.

            try
            {
                DataTable resultTable = DBUtility.GetSingleDBTableResult(connectionString, "SELECT * FROM MyDBTable");
                List<string> resultsList = new List<string>();
                foreach (DataRow dataRow in resultTable.Rows)
                {
                    resultsList.Add(dataRow[0].ToString());
                }
                return resultsList; 
            }
            finally
            {
                resultTable.Dispose();
            }
    

    As you can see from the code, the resultTable gets disposed for sure regardless of the return statement. The using block only makes sure that object gets disposed after the scope.

    Your first code looks ok to me and need not be changed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im really struggling to get my head round this. Im using c#. I want
I really want to get this to work, but I feel like I've been
I really want to have full control of section headers by laying out in
I really want Resharper to format my line-wrapped ternaries in this way return navigator.IsTerminating
Okay this is going to seem really dumb but bear with me please. A
I've been banging my head on this one for a while now. I want
I am building out an ajax enabled UI using webforms in asp.net. I really
I just can't get my head around this and possibly my title isn't perfectly
After two days of bashing my head and trying to figure this stuff out
Despite my background with asp .net C#, I've really tried to get my head

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.