Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
IDisposable Mysteries
Message
From
07/04/2011 10:20:11
 
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 8.0
OS:
Vista
Network:
Windows XP
Database:
Jet/Access Engine
Application:
Desktop
Miscellaneous
Thread ID:
01506168
Message ID:
01506368
Views:
38
>But, TBH, I had the impression that that was *not* the case - I thought that you could wrap any class in a using block and that the check for IDisposable was made at run time - i.e. Dispose() was called on classes that implemented it and ignored if they did not.
>
>No idea *why* I thought that though :-}
>OTOneH implementing it that way would not have caused problems at runtime ; OTOH catching it at compile time avoids having the compiler generate unneeded code.


I think the good thing about the way it works is in the case where you've created a class and *meant* to implement IDisposable but forgot ... you'd find out quick enough once you tried putting it in a using block.

>Another thing I only realized recently: within the same using block although you can instantiate multiple objects of the same type you cannot do so for different types. eg:
using (SomeClass s1 = new SomeClass(), s2 = new SomeClass()){}
>//is OK but this is not
>using(SomeClass s1 = new SomeClass(), SomeOtherClass s2 = new SomeOtherClass()){}
>Don't know why that restriction was neccessary....


I learned something new today ... I didn't know you could instantiate multiple objects in the using. The restriction that they be the same type is strange though ... probably not very often do you need multiple instances of the same type, but still it's good to know you can do that.

~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform