“使用”时会处置什么?使用了关键字
让我们举个例子:
using (var someObject = new SomeObject())
{
var someOtherObject = new SomeOtherObject();
someOtherObject.someMethod();
}
SomeOtherObject
也实现了 IDisposable。 当 SomeObject 被处置时,SomeOtherObject 也会被处置吗? SomeOtherObject 会发生什么? (SomeOtherObject的处置并没有在SomeObject的Dispose方法中实现)
Let's have an example:
using (var someObject = new SomeObject())
{
var someOtherObject = new SomeOtherObject();
someOtherObject.someMethod();
}
SomeOtherObject
also implements IDisposable.
Will be SomeOtherObject also disposed when SomeObject get disposed ? What will happen to the SomeOtherObject ?
(disposing of SomeOtherObject is not implemented in the Dispose method of SomeObject)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不会。只有 using 子句中的字段才会被处理。在你的情况下只有 someObject.
基本上该代码被翻译成
No. Only fields in the using clause will be disposed. In your case only someObject.
Basically that code gets translated into
不,
SomeOtherObject
将不会被处置。编译器将您的代码重组如下:
No,
SomeOtherObject
will not be Disposed.Your code is restructured by the compiler as follows:
没有 someOtherObject 不会被释放。
您的代码将转换为如下内容:
因此,不会对任何新创建的对象执行额外的调用。
No someOtherObject will not disposed.
Your code would traslates in something like this:
So, there are no additional calls to any newly created object would performed.
直接引用 MSDN :
因此,只有在 using 语句中声明和实例化的对象才会被释放。对于此类问题,我建议您在发布问题之前先做一些测试。
quote from MSDN directly:
Thus only the object declared and instantiated in the using statement will be disposed. For this kind of problem I would suggest you to do some test before post the question.
someOtherObject
将由垃圾收集器正常收集。如果您没有提供调用Dispose()
的适当终结器(析构函数),则永远不会调用此函数。当执行流程离开using
块时,只会调用someObject.Dispose()
。someOtherObject
will be collected normally by the Garbage Collector. If you did not provide an appropriate finalizer (destructor) that callsDispose()
, this will never get called. OnlysomeObject.Dispose()
will be called when execution flow leaves theusing
block.你应该这样写:
如果你的方法创建了很多一次性对象(在绘画代码中很常见),那么这可能会失控。重构为辅助方法或切换到显式的finally 块。
You should write it like this:
This can get out of hand if your method is creating a lot of disposable objects, common in painting code. Refactor into a helper method or switch to an explicit finally block.
当控制离开
using
块时,将调用someObject
引用的对象的Dispose
方法。您可以在 Dispose 方法中SuppressFinalize
,在这种情况下系统将不会调用该对象的finalizer
(否则它将调用)。但是,
someOtherObject
引用的对象将在适当的时间被 GC 收集,因为当控件离开块时,它不会被任何对象引用,并且将被标记为收集。The
Dispose
method of object that is referenced bysomeObject
will be called when control leaves theusing
block. You canSuppressFinalize
in Dispose method in which case system will not call that object'sfinalizer
(otherwise it will).The object that referenced by
someOtherObject
will, however, be collected by GC at appropriate time, because as the control leave the block, it won't be referenced by any object and will be marked for collection.不确定这是否是您来自的地方;
someOtherObject
在using
块之外无法访问;由于范围规则。编译器错误:“名称 v1 在当前上下文中不存在。”
即使遵循也会引发错误。
请参阅此和< a href="http://www.informit.com/library/content.aspx?b=STY_Csharp_24hours&seqNum=127" rel="nofollow noreferrer">此以获得更多帮助。
Not sure if this is where you are coming from; the
someOtherObject
is not going to be accessible outside theusing
block; because of the scoping rules.Compiler error: "The name v1 does not exist in the current context."
Even following would throw an error.
See this and this for more help.