27

When I refactor the following line:

Employee e = new Employee();
e.First = "Frank";
e.Last = "Rizzo";

using Resharper's "Use Object Initializer", I get the following:

Employee e = new Employee
             {
                 First = "Frank",
                 Last = "Rizzo"
             };

I really hate this type of formatting because with longer object names and variables it just gets out of control. How can I get Resharper to do the following?

Employee e = new Employee
{
    First = "Frank",
    Last = "Rizzo"
};
AngryHacker
  • 59,598
  • 102
  • 325
  • 594

3 Answers3

38

You can get very close to what you want in Resharper 4.5.

Resharper>Options

Languages>C#>Formatting Style>Other

under Other uncheck "Indent array, object and collection initializer block."

Handcraftsman
  • 6,863
  • 2
  • 40
  • 33
  • Using this with Version 5 gives 1 tab indent instead of 0. better than nothing... – mbx Aug 26 '11 at 13:37
  • 1
    found answer to that on http://stackoverflow.com/questions/747351/custom-brace-formatting-with-resharper – mbx Aug 26 '11 at 13:49
  • 1
    Does anyone have an answer for ReSharper 7.1.2? It is already unchecked but still indents wrongly. – John Jelinek Apr 22 '13 at 15:57
3

In Resharper 7 this option is here:

Resharper / Option / C# / Formatting Style / Braces Layout set Array and object initializer to: "At next line (BSD style)"

davidhq
  • 4,660
  • 6
  • 30
  • 40
  • 1
    I don't know about RS7, but in RS6 that's quite definitely not the right place — the setting in Formatting > Other determines whether the indent set here is relative to the start of the line or to the initializer statement. The live preview on each setting will show you what I mean better than words can explain it :) – Owen Blacker Sep 05 '13 at 13:25
  • @David Krmpotic is correct about RS7, and RS8 has it in the same place. – SWalters Feb 04 '14 at 14:51
  • In Reshaper 8 the setting to not indent the object initializer is Resharper / Option / C# / Formatting Style / Other / Align Multiline Constructs / Array, object and collection initializer – Graham Ambrose Mar 24 '15 at 12:05
1

In R# 2018.2.3 This is in:

ReSharper -> Options -> Code Editing -> C# -> Formatting Style -> Line Breaks And Wrapping -> Arrangement of Initializers

There is multiple settings you can play with there, namely:

  • Keep existing arrangement of initializers
  • Max object and collection initializer elements on a single line
Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118