1

In my below function...I want to return DataTable..

Should I convert the below line

DataTable table = CreateTable<T>();

to following

using(DataTable table = CreateTable<T>())
{
}

Function

public static DataTable ConvertTo<T>(IList<T> list)
{
    DataTable table = CreateTable<T>();

    int iColCount = table.Columns.Count;
    for (int j = 0; j < iColCount; j++)
    {
        DataColumn myDC = table.Columns[j];
        myDC.DataType = System.Type.GetType("System.String");
    }

    Type entityType = typeof(T);
    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
    foreach (T item in list)
    {
        DataRow row = table.NewRow();
        foreach (PropertyDescriptor prop in properties)
        {
            row[prop.Name] = prop.GetValue(item);
        } table.Rows.Add(row);
    }
    return table;
}

4 Answers4

2

Why would you?

using (obj)
{
    // Do something
}

is basically the same as:

try
{
    // Do something
}
finally
{
    obj.Dispose();
}

The point is to free the resources used by your object once you don't need it anymore. In your case, you're returning the datatable to the calling function, so you're still going to need it. Therefore, there's no reason to wrap your function inside of a using statement.

Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94
  • I want to return the object also. It should be return before disposing or after disposing ? –  Jul 26 '12 at 06:27
  • 1
    What I'm trying to say is: you should not return a disposed object. So if you're going to return it, don't dispose it. – Kevin Gosse Jul 26 '12 at 06:30
  • Ur code is disposing in Finally block. can you explain it more in details plz ? –  Jul 26 '12 at 06:55
  • My code is just the translation of what `using` does. I'm trying to explain that the `using` keyword is equivalent to calling the `Dispose` method inside of a finally block. Now, since you're returning your object, you mustn't call the `Dispose` method. Therefore, you mustn't use the `using` keyword either. – Kevin Gosse Jul 26 '12 at 11:23
0

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. If you want to use using ask this question "Is the object disposable?". If it is then using is the best option. You might wanna try to read more on this.

http://msdn.microsoft.com/en-us/library/yh598w02.aspx

Carls Jr.
  • 3,088
  • 7
  • 37
  • 57
0

You don't need to use using inside ConvertTo<T>(IList<T> list), but caller of ConvertTo<T>(IList<T> list) is responsible for disposing that DataTable.

using (var table = Test.ConvertTo(testList)) 
{
   // do stuff with DataTable
}

edit: from other side, there's this post, which states that in DataTable there's nothing to dispose even if it implements IDisposable: Should I Dispose() DataSet and DataTable?

Community
  • 1
  • 1
Giedrius
  • 8,430
  • 6
  • 50
  • 91
0

note that:

The using statement calls the Dispose method on the object and causes the object itself to go out of scope as soon as Dispose is called.

and Dispose releases all resources used by the DataTable.

so that you can not use using Statement and returning DataTable object in same time.

public static DataTable ConvertTo<T>(IList<T> list)
{
    DataTable resultTable = CreateTable<T>();

    using(DataTable table = CreateTable<T>())
    {
         ....
         resultTable = table;
    }

    // 'table' object disposed already
    return resultTable ;
}
Ria
  • 10,237
  • 3
  • 33
  • 60
  • Does it make sense to return that object that is already disposed ? –  Jul 26 '12 at 06:26