How-To's

Generating object initializers with ReSharper

ReSharper provides both a quick-fix and a context action to transform assignment statements into C# object initializers, and both are called “Use object initializer”.
 
So what’s the difference between them?
 
Suppose that you create an object with three assignment statements:
 

 
Putting the caret over the “new” keyword lets you apply a quick-fix that will put all assigned fields into an object initializer:
 

 
Putting the caret over the object variable or a field name in any of the tree assignment statements lets you apply a context action that will put only this assignment in the object initializer:
 

 
That is, using the context action, you can selectively put field assignments into the object initializer, leaving some of them to be declared explicitly.

Technorati tags: , , , ,

Discover more