문제

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