Dinesh
Dinesh

Reputation: 31

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.

Upvotes: 1

Views: 494

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156978

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.

Upvotes: 5

Related Questions