9

When creating a new type using the object initialiser syntax, Visual Studio automatically moves the braces to align with the type after the new keyword.

Like this

MyType myType = new MyType
                    {

                    };

However, I would prefer it not to do this and instead leave it like so

MyType myType = new MyType
{

};

Any ideas how to do this - I've been through all the Visual Studio formatting options and can't find one that is appropriate

Any ideas?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ChrisCa
  • 10,876
  • 22
  • 81
  • 118

2 Answers2

8

From my most popular answer here:

  1. In Braces Layout, set Array and object initializer to At Next line (BSD Style).
  2. In Other, make sure that Continuous line indent multiplier is set to 1.
  3. In Other, make sure that Indent array, object and collection initializer block is unchecked.

You will have the style that you want!

Community
  • 1
  • 1
Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
3

You will find this in Resharper options. Go to ReSharper -> Options -> C# -> Formatting Style -> Braces Layout -> "Array And Object Initialization". But I am not sure if ReSharper allows such formatting.

MichaelMocko
  • 5,466
  • 1
  • 21
  • 24
  • thanks -that seems like the most appropriate option, I agree. However, I have tried that one by changing it to be the same as a method declararion (No Space BSD). However, as soon as I hit return between the object initialisation braces {}, it immediately indents them up to after the new keyword – ChrisCa Feb 10 '12 at 09:57
  • Unfortunately ReSharper do not have style which You want to apply. You can only choose from those which are available there now :/. – MichaelMocko Feb 10 '12 at 10:07