Saturday, January 17, 2009

Interesting .NET Bugs: Object Initializers and using-statements

Ayende posted about this bug, but it's one hard to spot bug, so I felt it was valid to pass on.


If you use object intializers in the definition of a using statement it is normal to expect that if an exception occurs the dispose method will be called. After all that is the point of the pattern.




However, object initializers are compiled in an unexpected way here:

e


Beware.


(Both images stolen from Ayende. No harm intended.)

No comments: