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: , , , ,
Comments below can no longer be edited.

8 Responses to Generating object initializers with ReSharper

  1. Avatar

    hossein says:

    July 26, 2011

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

  2. Avatar

    Jura Gorohovsky says:

    July 28, 2011

    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. Avatar

    Valerie says:

    September 3, 2012

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

  4. Avatar

    Jura Gorohovsky says:

    September 3, 2012

    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. Avatar

    Tee says:

    November 14, 2012

    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.

  6. Avatar

    Jura Gorohovsky says:

    November 14, 2012

    Thank you @Tee )

  7. Avatar

    Nick Posey says:

    November 29, 2017

    I personally like using the ReSharper’s object initializer shortcut but it would be awesome if they would/could create a shortcut that would do the reverse (un-initialize an already initialized object). I find that it can sometimes be difficult to debug issues that arise inside of an initialized object and I often have to manually revert it back to individual property sets to find exactly which line (property) the error is occurring on. Especially if it’s an object containing other initialized objects. But formatting-wise, I prefer initialized objects.

  8. Avatar

    Aegis says:

    July 23, 2019

    @Nick Posey – not sure if it was an option when you posted your comment, but you can do the reverse now: click on the initialised object’s ‘new’ keyword, then click the resharper hammer icon in the lefthand tray (or use ctrl+. to bring up the Resharper menu) then select Convert To Assignment Statements.

    What I would love to see is the ability to have it generate default assignment statements from scratch for all the settable properties on the initialising object.

Discover more