For example:
using (disposable object here)
{
}
What determines if I can use an object in this way?
Would this work correctly?
using (WebClient webClient = new WebClient())
{
}
In order to be used in a using statement, a class needs to implement the IDisposable interface.
In your example, WebClient derives from Component, which implements IDisposable, so it would indeed work.
IDisposable is also acceptable, as is an implicit dynamic conversion.If a null resource is acquired, then no call to Dispose is made, and no exception is thrown.You can use it if the class implements the IDisposable interface. This keyword is basically just syntactic sugar for calling the object's IDisposable.Dispose() method automatically after the using block.
The Dispose() method:
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
The object provided in the using statement must implement the IDisposable interface.
This interface provides the Dispose method, which should release the object's resources.
Reference: http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx