This project is read-only.
5
Vote

Solve using statement + object/collection initializer issue

description

using (var resource = new Resource { Property = "InvalidValue" }) {
  // ...
}
This is the well-know issue in C#: when combining object initializer with using statement, you are risking to leak resource if exception happens during object initialization.

Maybe it is possible to 'fix' cases like this by 'tuning' using statement behavior in spec?

comments

VSadov wrote Jul 24, 2014 at 7:19 PM

Just like exceptions in constructors, exceptions in initializers are lexically outside of the protected block so will not be handled. And it is ByDesign right not both as written in spec and in the spirit of "using" statement.

VSadov wrote Jul 24, 2014 at 7:21 PM

I will keep this open for now to give more thought, but it is likely to be ByDesign.