Skip to content

The pattern book

Braces and bodies

dress_declaration_open_brace_position = next_line

Places the opening brace of a namespace, type, method, constructor, operator, property, indexer, event, accessor, or local function on the same line as its header, or on the next line. With the other open brace position rules, read in place of csharp_new_line_before_open_brace, which can then stay unset for tools that act on it, such as Visual Studio and Rider.

  • Values: same_line next_line
  • Default: next_line
  • Applies to: The opening brace of a namespace, type, member, accessor, or local function.
  • Guarantee: Only boundary whitespace changes; a brace decided by a more specific rule, such as embedded statement placement or initializer layout, is unchanged.
  • Shown with: csharp_indent_braces = false
Input
namespace Example {
class Point {
int X {
get {
return 0;
}
}
void Run() {
Work();
}
}
}
namespace Example {
class Point {
int X {
get {
namespace Example
{
class Point
{
int X
{
get
{
return 0;
}
}
void Run() {
void Run()
{
Work();
}
}
}
dress_statement_open_brace_position = next_line

Places the opening brace of a try, catch, finally, switch, checked, unsafe, or bare block on the same line as its header, or on the next line. A block that is an embedded statement follows dress_embedded_statement_placement when that is set. With the other open brace position rules, read in place of csharp_new_line_before_open_brace, which can then stay unset for tools that act on it, such as Visual Studio and Rider.

  • Values: same_line next_line
  • Default: next_line
  • Applies to: The opening brace of a statement block that is not an embedded statement.
  • Guarantee: Only boundary whitespace changes; a brace decided by a more specific rule, such as embedded statement placement or initializer layout, is unchanged.
  • Shown with: csharp_indent_braces = false
Input
try {
Work();
} catch (Exception) {
Recover();
}
switch (value) {
case 1: {
break;
}
}
try {
try
{
Work();
} catch (Exception) {
} catch (Exception)
{
Recover();
}
switch (value) {
case 1: {
switch (value)
{
case 1:
{
break;
}
}
dress_lambda_open_brace_position = next_line

Places the opening brace of a lambda or anonymous method body on the same line as the arrow or delegate keyword, or on the next line. With the other open brace position rules, read in place of csharp_new_line_before_open_brace, which can then stay unset for tools that act on it, such as Visual Studio and Rider.

  • Values: same_line next_line
  • Default: next_line
  • Applies to: The opening brace of a lambda or anonymous method body.
  • Guarantee: Only boundary whitespace changes; a brace decided by a more specific rule, such as embedded statement placement or initializer layout, is unchanged.
  • Shown with: csharp_indent_braces = false
Input
Action action = () => {
Work();
};
Action method = delegate {
Work();
};
Action action = () => {
Action action = () =>
{
Work();
};
Action method = delegate {
Action method = delegate
{
Work();
};
csharp_new_line_before_open_brace = unset

Controls owned token boundaries. Only boundary whitespace changes. Places only the braces no open brace position rule decided. Default preferences leave it out, since Visual Studio and Rider act on it too.

  • 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: unset (left out by dotnet dress init, which sets dress_declaration_open_brace_position, dress_statement_open_brace_position, dress_lambda_open_brace_position instead)
  • 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.

dress_single_line_blocks = expand_unless_trivial

Keeps a block, accessor list, type, namespace, or switch that is written on one line as it is, expands it, or expands all but the trivial ones: an empty block and an accessor list of auto accessors. Read in place of csharp_preserve_single_line_blocks, which can then stay unset for tools that act on it, such as Visual Studio and Rider.

  • Values: preserve expand expand_unless_trivial
  • Default: expand_unless_trivial
  • Applies to: Existing single-line blocks and accessor lists.
  • Guarantee: Preserve leaves them as written; expand_unless_trivial keeps empty blocks and auto-accessor lists on one line.
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_preserve_single_line_blocks = unset

Controls existing single-line blocks and accessor lists. False expands safe single-line blocks and accessor lists. Read only while dress_single_line_blocks is unset. Default preferences leave it out, since Visual Studio and Rider act on it too.

  • Values: true false
  • Default: unset (left out by dotnet dress init, which sets dress_single_line_blocks instead)
  • 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() { } }

Code unchanged.

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() { } }
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 the namespace declaration form in a file with a single namespace. 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 statement and its expression-bodied form represent the same expression.

  • Values: block expression
  • Default: expression
  • Applies to: Lambda bodies.
  • Guarantee: A single return 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();
}