8

When doing something like this:

MyObject tmp = new MyObject();
tmp.Prop = "Hello";

ReSharper tells me to 'Use object initializer', so I let it reformat the code, and I get something like this:

MyObject tmp = new MyObject {
                                Prop = "Hello"
                            };

However, I would like the first brace to be on the second line, like this:

MyObject tmp = new MyObject 
                            {
                                Prop = "Hello"
                            };

But I can't find any setting for this anywhere. I have the setting 'C# -> Formatting Style -> Braces Layout -> Array and object initializer' set to 'At next line (BSD style)'

Could there be some other setting interfering with this, preventing the formatting I want?

Edit: If I manually format the code like I want it, it will automatically reformat (to the wrong format) when I enter the semi colon.

Joel
  • 8,502
  • 11
  • 66
  • 115
  • I've recently installed StyleCop, if that changes anything... – Joel Feb 08 '13 at 07:33
  • 1
    Maybe this question/answer can help you: [Resharper C# Formatting Style shows “new” on new line instead of same line when chopping long lines](http://stackoverflow.com/questions/13985739/resharper-c-sharp-formatting-style-shows-new-on-new-line-instead-of-same-line/13987602#13987602) – khellang Feb 08 '13 at 10:54
  • @KristianHellang It sure helped. – Joel Feb 08 '13 at 11:43
  • 1
    @KristianHellang But I still can't get the formatting I want... – Joel Feb 08 '13 at 12:04
  • That is very strange, I have all options at C# -> Formatting Style -> Braces Layout set to 'At next line (BSD style)', and it formats as you want. Do you have all of them set to this value? Please try to set them that way. Also please try to disable StyleCop. – Dmitry Osinovskiy Feb 08 '13 at 18:26
  • The path to the setting was instantly helpful! – Jens Mander Oct 04 '18 at 00:47

2 Answers2

11

So I finally found the setting that was messing with the formatting:

enter image description here

Joel
  • 8,502
  • 11
  • 66
  • 115
  • 9
    And if you have multiple properties and you want each to go on a new line, also select to `Code Editing -> C# -> Formatting Style -> Line Breaks and Wrapping -> Line Wrapping -> Wrap object and collection initializer => Chop Always`. – Peter Majeed Mar 11 '13 at 14:47
4

Sometimes i need to set the same option in VS and Resharper.

In Resharper it's like you said:

C# -> Formatting Style -> Braces Layout -> Array and object initializer' set to 'At next line (BSD style)

In Visual Studio:

Tools -> Options... -> Text Editor -> C# -> Formatting -> New Lines -> Place open brace on new line for object initialzers
Malmi
  • 387
  • 3
  • 8
  • 1
    Without the VS tweak in addition to the RS tweak, reformatting was still doing it wrong, thank you! – Darryl Jul 12 '13 at 16:15