Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
IDisposable Mysteries
Message
From
07/04/2011 04:35:52
 
 
To
06/04/2011 21:01:28
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:
01506337
Views:
42
>>A class that implements IDisposable indicates to anything concerned that it should call Dispose(). This is, essentially, how the compiler translates the presence of a 'Using' block - if the created class implements IDisposable then it will automatically call Dispose() on that class at the end of the block.
>
>And, in fact, it should be noted that if a class does not implement IDisposable, then you can't even have it in a using statement, you'll get a compiler error.

True.
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.

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....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform