This report is based on this blog post:
http://ayende.com/Blog/archive/2009/01/15/avoid-object-initializers-amp-the-using-statement.aspxThe behavior if the microsoft c# compiler behaves different then the most users expected.
The most users expect that this code:
using(var rijndael = new RijndaelMandaged
{
Key = Key,
IV = Convert.FromBase64String(attribute.Value),
Mode = CipherMode.CBS
})
is transformed to this:
using(var rijndael = new RijndaelMandaged() )
{
rijndael.Key = Key;
rijndael.IV = Convert.FromBase64String(attribute.Value);
rijndael.Mode = CipherMode.CBS;
}
but the c# compiler creates this:
var tmp = new RijndaelMandaged();
tmp.Key = Key;
tmp.IV = Convert.FromBase64String(attribute.Value);
tmp.Mode = CipherMode.CBS;
using(var rijndael = tmp)
{
}
Since this could be result in an unexpected behavior, it should be avoided and it would be nice if ReSharper gets an Warning for that.
PS: This also seems to happen of a user uses dose this: using(var thing = new Thing().WithWheels())