static DataTable getDataTable() { ")) { SqlCommand com = new SqlCommand("", con); SqlDataAdapter sda = new SqlDataAdapter(com); using (DataTable dt = new DataTable()) { sda.Fill(dt); return dt; } } }
Reflector 反编译后
private static DataTable getDataTable() { DataTable table2; ")) { SqlCommand selectCommand = new SqlCommand("", connection); SqlDataAdapter adapter = new SqlDataAdapter(selectCommand); using (DataTable table = new DataTable()) { adapter.Fill(table); table2 = table; } } return table2; } private static DataTable getDataTable() { DataTable table2; ")) { SqlCommand selectCommand = new SqlCommand("", connection); SqlDataAdapter adapter = new SqlDataAdapter(selectCommand); using (DataTable table = new DataTable()) { adapter.Fill(table); table2 = table; } } return table2; }
using 中写 return 一样会释放using 中对象 但是会在外面定义一个一样的对象 赋值后 释放 最后 return 外面定义的那个对象