Skip to content

The pattern book

Braces and bodies

csharp_new_line_before_open_brace = accessors,anonymous_methods,anonymous_types,control_blocks,events,indexers,lambdas,local_functions,methods,properties,types

Controls owned token boundaries. Only boundary whitespace changes.

  • Values: accessors anonymous_methods anonymous_types control_blocks events indexers lambdas local_functions methods object_collection_array_initializers properties types all none — any comma-separated selection.
  • Default: accessors,anonymous_methods,anonymous_types,control_blocks,events,indexers,lambdas,local_functions,methods,properties,types
  • Applies to: Owned token boundaries.
  • Guarantee: Only boundary whitespace changes.
  • Shown with: csharp_indent_braces = false
class Example {
int Count {
get {
get
{
return 0;
}
}
}
csharp_new_line_before_else = true

Controls owned token boundaries. Only boundary whitespace changes.

  • Values: true false
  • Default: true
  • Applies to: Owned token boundaries.
  • Guarantee: Only boundary whitespace changes.
Input
if (ready) { Work(); } else { Wait(); }
if (ready) { Work(); } else { Wait(); }
if (ready) { Work(); }
else { Wait(); }
csharp_new_line_before_catch = true

Controls owned token boundaries. Only boundary whitespace changes.

  • Values: true false
  • Default: true
  • Applies to: Owned token boundaries.
  • Guarantee: Only boundary whitespace changes.
Input
try { Work(); } catch (Exception) { Recover(); } finally { CleanUp(); }
try { Work(); } catch (Exception) { Recover(); } finally { CleanUp(); }
try { Work(); }
catch (Exception) { Recover(); } finally { CleanUp(); }
csharp_new_line_before_finally = true

Controls owned token boundaries. Only boundary whitespace changes.

  • Values: true false
  • Default: true
  • Applies to: Owned token boundaries.
  • Guarantee: Only boundary whitespace changes.
Input
try { Work(); } catch (Exception) { Recover(); } finally { CleanUp(); }
try { Work(); } catch (Exception) { Recover(); } finally { CleanUp(); }
try { Work(); } catch (Exception) { Recover(); }
finally { CleanUp(); }

New line before members in object initializers

Section titled “New line before members in object initializers”
csharp_new_line_before_members_in_object_initializers = true

Controls owned token boundaries. Only boundary whitespace changes.

  • Values: true false
  • Default: true
  • Applies to: Owned token boundaries.
  • Guarantee: Only boundary whitespace changes.
Input
var value = new Example { First = 1, Second = 2 };
var value = new Example { First = 1, Second = 2 };
var value = new Example {
First = 1,
Second = 2
};

New line before members in anonymous types

Section titled “New line before members in anonymous types”
csharp_new_line_before_members_in_anonymous_types = true

Controls owned token boundaries. Only boundary whitespace changes.

  • Values: true false
  • Default: true
  • Applies to: Owned token boundaries.
  • Guarantee: Only boundary whitespace changes.
Input
var value = new { First = 1, Second = 2 };
var value = new { First = 1, Second = 2 };
var value = new {
First = 1,
Second = 2
};
csharp_new_line_between_query_expression_clauses = true

Controls owned token boundaries. Only boundary whitespace changes.

  • Values: true false
  • Default: true
  • Applies to: Owned token boundaries.
  • Guarantee: Only boundary whitespace changes.
Input
var result = from item in items where item.Active select item.Name;
var result = from item in items where item.Active select item.Name;
var result = from item in items
where item.Active
select item.Name;

Multiline parameter list open brace position

Section titled “Multiline parameter list open brace position”
dress_multiline_parameter_list_open_brace_position = same_line

Places a declaration body brace on the same line as, or the line after, an own-line multiline parameter-list closing parenthesis. Applies to primary constructors without base lists, methods, and constructors without initializers.

  • Values: same_line next_line
  • Default: same_line
  • Applies to: The boundary between an own-line multiline parameter-list closing parenthesis and its directly following declaration body brace.
  • Guarantee: Base lists, constructor initializers, constraints, and declarations without an own-line parameter-list close are unchanged.
Input
class Example
{
void Run(
int value
) {
}
}

Code unchanged.

csharp_preserve_single_line_blocks = false

Controls existing single-line blocks and accessor lists. False expands safe single-line blocks and accessor lists.

  • Values: true false
  • Default: false
  • Applies to: Existing single-line blocks and accessor lists.
  • Guarantee: False expands safe single-line blocks and accessor lists.
Input
class Example { void Run() { Work(); } void Work() { } }
class Example { void Run() { Work(); } void Work() { } }
class Example {
void Run() {
Work();
} void Work() {
}
}
csharp_preserve_single_line_statements = false

Controls adjacent statements and member declarations. False separates safe adjacent statements and members.

  • Values: true false
  • Default: false
  • Applies to: Adjacent statements and member declarations.
  • Guarantee: False separates safe adjacent statements and members.
Input
class Example { void Run() { Work(); } void Work() { } }
class Example { void Run() { Work(); } void Work() { } }
class Example { void Run() { Work(); }
void Work() { } }
dress_preserve_trivial_single_line_blocks = true

Controls existing single-line empty blocks and auto-accessor lists. True keeps empty blocks and accessor lists without bodies on one line when single-line blocks expand.

  • Values: true false
  • Default: true
  • Applies to: Existing single-line empty blocks and auto-accessor lists.
  • Guarantee: True keeps empty blocks and accessor lists without bodies on one line when single-line blocks expand.
  • Shown with: csharp_preserve_single_line_blocks = false
Input
class Example { bool Ready { get; } void Run() { } void Work() { Ready = true; } }
class Example { bool Ready { get; } void Run() { } void Work() { Ready = true; } }
class Example {
bool Ready { get; } void Run() { } void Work() {
Ready = true;
}
}
csharp_preferred_modifier_order = public,protected,internal,private,file,new,static,abstract,virtual,sealed,override,readonly,unsafe,required,volatile,async

Controls member modifier lists. Only modifier token order changes.

  • Values: public protected internal private file new static abstract virtual sealed override readonly unsafe required volatile async — every item, in any order, comma-separated.
  • Default: public,protected,internal,private,file,new,static,abstract,virtual,sealed,override,readonly,unsafe,required,volatile,async
  • Applies to: Member modifier lists.
  • Guarantee: Only modifier token order changes.
Input
class Example { readonly public static int Value; }
class Example { readonly public static int Value; }
class Example { public static readonly int Value; }
dress_namespace_style = file_scoped

Controls compilation-unit namespace declaration. The namespace name, externs, usings, attributes, and members are preserved while only the namespace delimiter form changes.

  • Values: file_scoped block_scoped
  • Default: file_scoped
  • Applies to: Compilation-unit namespace declaration.
  • Guarantee: The namespace name, externs, usings, attributes, and members are preserved while only the namespace delimiter form changes.
Input
namespace Example
{
class Value { }
}
namespace Example
{
namespace Example;
class Value { }
}
dress_method_body = unset

Controls method bodies. The selected body form preserves the represented statement or returned expression.

  • Values: block expression
  • Default: unset (the rule makes no change unless you set it)
  • Applies to: Method bodies.
  • Guarantee: The selected body form preserves the represented statement or returned expression.
Input
class Example { int Value() { return 1; } }

Code unchanged.

dress_constructor_body = unset

Controls constructor bodies. The selected body form preserves the represented statement or returned expression.

  • Values: block expression
  • Default: unset (the rule makes no change unless you set it)
  • Applies to: Constructor bodies.
  • Guarantee: The selected body form preserves the represented statement or returned expression.
Input
class Example { int value; public Example(int value) { this.value = value; } }

Code unchanged.

dress_operator_body = unset

Controls operator bodies. The selected body form preserves the represented statement or returned expression.

  • Values: block expression
  • Default: unset (the rule makes no change unless you set it)
  • Applies to: Operator bodies.
  • Guarantee: The selected body form preserves the represented statement or returned expression.
Input
class Example { public static Example operator +(Example a, Example b) { return a; } }

Code unchanged.

dress_property_body = expression

Controls property bodies. The selected body form preserves the represented statement or returned expression.

  • Values: block expression
  • Default: expression
  • Applies to: Property bodies.
  • Guarantee: The selected body form preserves the represented statement or returned expression.
Input
class Example { int Value { get { return 1; } } }
class Example { int Value { get { return 1; } } }
class Example { int Value => 1; }
dress_indexer_body = expression

Controls indexer bodies. The selected body form preserves the represented statement or returned expression.

  • Values: block expression
  • Default: expression
  • Applies to: Indexer bodies.
  • Guarantee: The selected body form preserves the represented statement or returned expression.
Input
class Example { int this[int index] { get { return index; } } }
class Example { int this[int index] { get { return index; } } }
class Example { int this[int index] => index; }
dress_accessor_body = expression

Controls accessor bodies. The selected body form preserves the represented statement or returned expression.

  • Values: block expression
  • Default: expression
  • Applies to: Accessor bodies.
  • Guarantee: The selected body form preserves the represented statement or returned expression.
Input
class Example { int Value { get { return 1; } set { Store(value); } } }
class Example { int Value { get { return 1; } set { Store(value); } } }
class Example { int Value { get => 1; set => Store(value); } }
dress_lambda_body = expression

Controls lambda bodies. A single return or expression statement and its expression-bodied form represent the same expression.

  • Values: block expression
  • Default: expression
  • Applies to: Lambda bodies.
  • Guarantee: A single return or expression statement and its expression-bodied form represent the same expression.
Input
class Example
{
Func<int> Value = () => { return 1; };
}
class Example
{
Func<int> Value = () => { return 1; };
Func<int> Value = () => 1;
}
dress_embedded_statement_placement = next_line

Place the body of if, else, loops, using, lock, and fixed on the same line or a new line. For example: if (condition) return false;. Applies with or without braces. Embedded statement placement: single-line if, inline return, return new line.

  • Values: same_line next_line
  • Default: next_line
  • Applies to: The boundary before brace-optional embedded statements.
  • Guarantee: Only boundary whitespace changes.
Input
class Example
{
void Run() { if (true) Work(); }
void Work() { }
}
class Example
{
void Run() { if (true) Work(); }
void Run() { if (true)
Work(); }
void Work() { }
}
dress_embedded_statement_braces = balanced

Controls brace-optional embedded statements. Brace changes preserve control flow and use planned body layout.

  • Values: compact balanced always
  • Default: balanced
  • Applies to: Brace-optional embedded statements.
  • Guarantee: Brace changes preserve control flow and use planned body layout.
Input
class Example
{
void Run() { if (true) Work(); }
void Work() { }
}

Code unchanged.

dress_braces_for_multiline_statement_header = true

Controls multiline statement headers owning brace-optional embedded statements. A multiline statement header contributes only a brace constraint.

  • Values: true false
  • Default: true
  • Applies to: Multiline statement headers owning brace-optional embedded statements.
  • Guarantee: A multiline statement header contributes only a brace constraint.
Input
if (firstCondition
&& secondCondition)
Work();
if (firstCondition
&& secondCondition)
{
Work();
}