C# trivia: what happens if you wrap an object which doesn't implement IDisposable within a using block?
Ans: Nothing, the code compiles alright and runs just fine; the problem occurs if you assumed that only IDisposable objects can be wrapped within a using block and once the object goes out of using block, the Dispose() on it would be automatically called and hence freeing up your resources; just like I assumed when I wrapped a XmlDataReader within the using block; (wrongfully) thinking that the connection associated with it will automagically be closed once xmldr goes out of scope. Unfortunately; everything worked just fine till one day I started getting Connection pool exceptions and realized that any connection used by xmldr is not getting closed and had to explicitly close them within a finally block! Ideally, only objects implementing IDisposable should be allowed to be wrapped within a using block and you should at least get a compile time warning (if not error) in case you try to place a non-Idisposable object.
Aside: I guess you can wrap me within the using{}, cause I-sure-am-Disposable!
Hello Sachin.. u really are cool..
ReplyDeleteI am mostly like u.. especially the process monitor way and the other thoughts...
I am trying to build a network of people like us online.. Friends ?
What is your email address ? Orkut profile ?
http://www.orkut.co.in/Profile.aspx?uid=5318998369738533880
quakeboy,
ReplyDeleteI stay away from social networking sites (including orkut)...guess am too old for all the networking stuff :-)