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.

This entry was posted in How-To's, ReSharper Tips&Tricks and tagged . Bookmark the permalink.

6 Responses to Generating object initializers with ReSharper

  1. hossein says:

    how can i adapt resharper when i cleapup my code Generating object initializer?

  2. @hossein
    AFAIK this transformation can’t be executed automatically with Code Cleanup.
    If you’re using ReSharper 6, once you land in a place where ReSharper suggests transforming to an object initializer, press Alt+Enter and select “Find similar issues”: this will give you a list of all places in a specified scope where object initializers can be generated, and you’ll be able to quickly browse between these places and generate the initializers using quick-fixes.

  3. Valerie says:

    Resharper is rubbish. Why do u think that object initializer is better than setting fields directly?

  4. @Valerie
    Personally, I think so because it’s way more compact syntax.
    As to ReSharper, you can always opt to use or decline its suggestions, it isn’t meant to impose itself on you.
    “ReSharper is rubbish” is something we’re hearing pretty consistently from rookie users. Now look at them after a couple of months.

  5. Tee says:

    After a week of ReSharper using, I used to say the same “ReSharper is rubbish”, because there was too much suggestions, fixes, code formating…, that I was not used to.
    But after two weeks of ReSharper using, I started to say “Hmmm, this is clever” or “Uf, I’ve missed this and that, thank you ReSharper for your help”.
    Is ReSharper only for begginers, who lack coding basics and patterns ? I don’t think so. We learn the whole life and errors or bugs are part of our lives. Life is easier, when there is something, that can be used to check our work.
    Advertisement ? Maybe, but I have to admit, that ReSharper is pretty damn good thing.
    All that I can say is, that my English is rubbish. Any ReEnglisher available ? :)
    P.S. ReSharper can be very easily disabled after all. Don’t use it, if you don’t like it.
    P.P.S. Sorry for offtopic.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>