I've been rewriting some old code so that the using
statement is used for my DataTables, instead of remembering to Dispose
each time:
using (DataTable dt = BLL.GetDataTable()) {
foreach(DataRow dr in dt.Rows) {
// iteration logic
}
}
However in one particular case, the DataTable content differs based on a variable, so I create the initial DataTable and then assign the value afterwards:
DataTable dt = new DataTable();
switch(foo) {
case bar:
dt = BLL.GetDataTable(bar);
break;
default:
dt = BLL.GetDataTable();
break;
}
// iteration logic here
dt.Dispose();
Changing this to use using
, I have:
using (DataTable dt = new DataTable()) {
switch(foo) {
case bar:
dt = BLL.GetDataTable(bar);
break;
default:
dt = BLL.GetDataTable();
break;
}
// iteration logic here
}
Is that good practice (i.e. creating the empty
DataTable with a using
statement)? I don't know why but it doesn't feel quite right.