简体   繁体   中英

Can we use Using statement for objects which does not implement IDisposable

I have service which does not implement IDisposable in its class. While creating object for class, I putting object creation statement in Using statement. 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.

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'

The easy reason you won't see it implements IDisposable is that one of its base classes implements IDisposable .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM