由于SqlConnection
和SqlCommand
implement IDisposable
,我将它们包装在using
语句中。
但是,我在方法中创建它们,然后将它们传递给using
语句,因此想知道右括号是否using
会处理SqlCommand
由方法创建的。
所以我尝试了这个:
private void button1_Click(object sender, EventArgs e)
{
SqlCommand command = new SqlCommand();
command.CommandTimeout = 10;
command.Disposed += s_Disposed;
using (SqlCommand tempCommand = command) { }//This is where even "command" is supposed to get disposed.
Text = command.CommandTimeout.ToString();
}
void s_Disposed(object sender, EventArgs e)
{
MessageBox.Show("Disposed");
}
并且惊讶地看到MessageBox
确实显示暗示它已被处置,但文本是“10”。
为什么?