Object initializer format

Does anyone know the settings to use so that object initializers are left aligned with the declaration, like this?

Blah blah = new Blah
{
    Foo = foo,
    Bar = bar
};

I've experimented with the various options and the closest I can get is the braces aligned on the constructor:

Blah blah = new Blah
                {
                     Foo = foo,
                    Bar = bar,
                }

 
 
]]>

9 comments

Options-> ... ->Formatting Style -> Other -> Indent array, object and collection initializer block

--
Sergey V. Coox
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Jetbrains" <lothan@newsguy.com> wrote in message news:gnkel1$tsj$1@is.intellij.net...
Does anyone know the settings to use so that object initializers are left aligned with the declaration, like this?

Blah blah = new Blah
{
    Foo = foo,
    Bar = bar
};

I've experimented with the various options and the closest I can get is the braces aligned on the constructor:

Blah blah = new Blah
                {
                     Foo = foo,
                    Bar = bar,
                }

 
 
]]>

0

That was the first setting I tried, but setting it to BSD style actually looks a lot closer to GNU style than BSD. I had expected BSD style to left align with the declaration and GNU style to be indented, but both are indented. Seems the only difference is BSD style currently aligns with the new operator whereas GNU seems to indent two tabs or something like that. I even turned off "Align multiple constructs: Array, object and collection initializer" but this setting has no effect.
"Sergey V. Coox (JetBrains)" <qx@intellij.com> wrote in message news:gnm0on$ges$1@is.intellij.net...
Options-> ... ->Formatting Style -> Other -> Indent array, object and collection initializer block

--
Sergey V. Coox
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Jetbrains" <lothan@newsguy.com> wrote in message news:gnkel1$tsj$1@is.intellij.net...
Does anyone know the settings to use so that object initializers are left aligned with the declaration, like this?

Blah blah = new Blah
{
    Foo = foo,
     Bar = bar
};

I've experimented with the various options and the closest I can get is the braces aligned on the constructor:

Blah blah = new Blah
                {
                     Foo = foo,
                    Bar = bar,
                }

 
 
]]>

0

Whas this bug fixed?

I really would like to have my constructor like:

StudentName student = new StudentName {      FirstName = "John",      LastName = "Smith",      ID = 116 };

0

1) To format as in first topic message, go to Braces Layout -> Braces Layout -> Array and object initializer and set it to "At next line (BSD style)".
2) To format as in your sample, set the same parameter to "At end of line (K&R style)" and uncheck Others -> Align Multiline Constructs -> Array, object and collection initializer.

0

It seems, that the setting does not work as expected.

0

@K what exactly does not work for you here? 

0

Hi Alexander,

Please have a look at my screenshot. There is unwanted indent even with BSD style.

Turns out, StyleCop force that... If I uninstall it, everything works fine.

Any ideas how to fix it?

I want sometime time that:

var x = new X

{

    Id = 1

};

0

@K you might untick "ReSharper | Options | Code Editing | C# | Formatting Style | Tabs, Indents, Alignment | Align Multiline Constructs | Array, object and collection initializer" checkbox after installing the plugin.

0

Hi Alexander,

Yeah, that did the trick for me. Thank you.

0

Please sign in to leave a comment.