Project
ReSharper
Priority
Normal
Type
Feature
Fix versions
Mirabile Futurum
State
Open
Assignee
Ilya Ryzhenkov
Subsystem
Code Analysis
Affected versions
No Affected versions
Fixed in build
No Fixed in build
  • Created by   Steve Wagner
    3 years ago (23 Jan 2009 14:51)
  • Updated by   Kirill Falk
    12 months ago (05 May 2011 17:31)
  • Jira: RSRP-92815
    (history, comments)
 
RSRP-92815 Warning if object initializers are used in a using statement
7
Issue is visible to: All Users
  The issue is visible to the selected user group only
This report is based on this blog post:
http://ayende.com/Blog/archive/2009/01/15/avoid-object-initializers-amp-the-using-statement.aspx

The 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())
Comments (0)
 
History
 
Linked Issues (?)
 
TeamCity Changes (0)
 
Issue has no comments