Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the formatting of the "Use Object Initializer" refactoring in Resharper?

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"
};
like image 374
AngryHacker Avatar asked Jan 20 '10 20:01

AngryHacker


3 Answers

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

like image 129
Handcraftsman Avatar answered Oct 20 '22 20:10

Handcraftsman


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)"

like image 32
davidhq Avatar answered Oct 20 '22 20:10

davidhq


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
like image 1
Michal Ciechan Avatar answered Oct 20 '22 22:10

Michal Ciechan