>>>>>>Now, let me give you a little brain teaser
>>>>>>
>>>>>>What is an elegant way to dispose an object IF it implements IDisposable.
>>>>>> You do not know whether the object implements IDisposable
>>>>>>
>>>>>>
>>>>>>public void SomeMethod(ISomeInterface theObject)
>>>>>>{
>>>>>> // do something with theObject
>>>>>> // and if theObject implements IDisposable
>>>>>> // dispose it
>>>>>>}
Can't think of anything beyond the obvious:
if (theObject is IDisposable) ((IDisposable)theObject).Dispose();
>>>>>//or
>>>>>IDisposable d = theObject as IDisposable;
>>>>>if (d != null) d.Dispose();
But presumably you've seen something else ? :-}
>>>>
>>>>
>>>>Seen something else ? Yes
>>>>
>>>>It's the latter of the obvious, but the compiler does it for you
>>>>
>>>>
>>>>public void SomeMethod(ISomeInterface theObject)
>>>>{
>>>> using (theObject as IDisposable)
>>>> {
>>>> // do something here
>>>>
>>>>
>>>> } //dispose
>>>>}
>>>>
>>>>pages 32-33
http://www.amazon.co.uk/More-Effective-Specific-Software-Development/dp/0321485890/ref=sr_1_1?ie=UTF8&s=books&qid=1295806114&sr=8-1>>>>
>>>>Also good to read
http://www.amazon.co.uk/Effective-covers-4-0-Specific-Development/dp/0321658701/ref=sr_1_3?ie=UTF8&s=books&qid=1295806114&sr=8-3#_>>>
>>>Ah. I considered using 'using' but obviously didn't understand how this was implemented. ie. I assumed that the potential
using(null)
>>>{
>>>}
> would cause an exception
>>
>>
>>The compiler creates its own (hidden) variable and compares it to null at the end of the using If it is not null, Dispose() is called
>
>Yeah. I understand what it must be doing - just didn't know that it did it (i.e accepted a null argument)). The documentation doesn't mention this - in fact it explicitly states : "The object provided to the using statement must implement the IDisposable interface" - no mention of accepting null,,,,,,
(1) > The object provided to the using statement must implement the IDisposable interface"
It does since we write : using ( theObject as IDisposable)
(2) > no mention of accepting null,,,,,,
Not obvious, I had to dig a bit
http://msdn.microsoft.com/en-us/library/aa664736(VS.71).aspx
A using statement is translated into three parts: acquisition, usage, and disposal. Usage of the resource is implicitly enclosed in a try statement that includes a finally clause. This finally clause disposes of the resource. If a null resource is acquired, then no call to Dispose is made, and no exception is thrown.
A using statement of the form
using (ResourceType resource = expression) statement
corresponds to one of two possible expansions. When ResourceType is a value type, the expansion is
{
ResourceType resource = expression;
try {
statement;
}
finally {
((IDisposable)resource).Dispose();
}
}
Otherwise, when ResourceType is a reference type, the expansion is
{
ResourceType resource = expression;
try {
statement;
}
finally {
if (resource != null) ((IDisposable)resource).Dispose();
}
}
Interesting
A using statement of the form
using (expression) statement
has the same two possible expansions, but in this case ResourceType is implicitly the compile-time type of the expression, and the resource variable is inaccessible in, and invisible to, the embedded statement.
Gregory