[英]Can we use Using statement for objects which does not implement IDisposable
I have service which does not implement IDisposable in its class. 我有没有在其类中实现IDisposable的服务。 While creating object for class, I putting object creation statement in Using statement. 在为类创建对象时,我将对象创建语句放在Using语句中。 I executed the code, it not giving any error. 我执行了代码,没有给出任何错误。 Can you explain why the creation object which does not implement IDisposable not throwing exception in using statement. 您能解释为什么未实现IDisposable的创建对象在using语句中不抛出异常。
No. That is not possible. 不,那是不可能的。
If you use this code: 如果使用此代码:
using (object o = new object())
{ }
You receive: 你收到:
'object': type used in a using statement must be implicitly convertible to 'System.IDisposable' '对象':using语句中使用的类型必须隐式转换为'System.IDisposable'
The easy reason you won't see it implements IDisposable
is that one of its base classes implements IDisposable
. 您看不到它实现IDisposable
的简单原因是它的基类之一实现了IDisposable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.