【发布时间】:2013-09-25 20:44:38
【问题描述】:
为什么在 if 语句中使用对象ok 时会超出范围?以及如何处置对象ok?
public class hello : IDisposable {
}
public class hi{
private void b()
{
using(hello ok = new hello());
hello no = new hello();
if( ok == no )
{
ok = no;
}
}
}
【问题讨论】:
-
object ok 将在
using作用域之后被释放在这里阅读msdn.microsoft.com/en-us/library/yh598w02.aspx 但你需要把{括号来定义作用域和实现IDisposable -
在 {} 中使用 using 语句后的所有内容。例如 - using(var a = new b()){if (a == 1)a = 2;};
-
你的 using 块需要被包裹在 {...}
标签: c# .net memory-leaks idisposable