Вопрос

which approaches are correct about "using" (first or second)?

First:

using (DataTable dt = list.ToDataTable())
{
     dataList.DataSource = dt;
     dataList.DataBind();
}

Second:

 using (DataTable dt = list.ToDataTable())
 {
     dataList.DataSource = dt;
 }
 dataList.DataBind();
Это было полезно?

Решение

Well in your case it has to be the first approach otherwise you will dispose of the DataTable before it's been used (so DataBind() in the second would throw an exception).

In general though, you only need to keep code which uses the disposable object within the scope of the using.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top