简体   繁体   English

我们可以对未实现IDisposable的对象使用Using语句吗

[英]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.

相关问题 处理所有实现IDisposable的嵌套对象 - Dispose of nested objects which all implement IDisposable NUnit是否处置实现IDisposable的对象? - Does NUnit dispose of objects that implement IDisposable? 是否有一个为using语句实现IDisposable的公共对象列表? - Is there a list of common object that implement IDisposable for the using statement? C#/ VB.NET using语句实际上对未实现IDisposable的类做任何事情 - Does C#/VB.NET using statement actually do anything with classes that do not implement IDisposable 如果一个成员是 IDisposable,我们是否应该实施 IDisposable - Should we implement IDisposable if one member is IDisposable CLR如何找到实现IDisposable的类? - How does the CLR find the classes which implement IDisposable? 对不使用 IDisposable 的框架组件使用 .NET 分析器规则 CA1001 - Using .NET analyzer rule CA1001 for components of a framework which does not use IDisposable 使用语句和IDisposable接口 - Using statement and the IDisposable interface 哪个更好,何时:使用语句或在C#中的IDisposable上调用Dispose()? - Which is better, and when: using statement or calling Dispose() on an IDisposable in C#? 我们可以在不实现IDisposable接口的情况下使用Dispose方法吗? - Can we use Dispose method without Implementing IDisposable Interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM