The pattern book
Braces and bodies
Open brace position
Section titled “Open brace position”Declaration open brace position
Section titled “Declaration open brace position”dress_declaration_open_brace_position = next_linePlaces 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_linenext_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
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(); } }}Code unchanged.
Statement open brace position
Section titled “Statement open brace position”dress_statement_open_brace_position = next_linePlaces 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_linenext_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
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; }}Code unchanged.
Lambda open brace position
Section titled “Lambda open brace position”dress_lambda_open_brace_position = next_linePlaces 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_linenext_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
Action action = () => { Work();};Action method = delegate { Work();};Action action = () => {Action action = () =>{ Work();};Action method = delegate {Action method = delegate{ Work();};Code unchanged.
Newlines
Section titled “Newlines”New line before open brace
Section titled “New line before open brace”csharp_new_line_before_open_brace = unsetControls 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:
accessorsanonymous_methodsanonymous_typescontrol_blockseventsindexerslambdaslocal_functionsmethodsobject_collection_array_initializerspropertiestypesallnone— any comma-separated selection. - Default:
unset(left out bydotnet dress init, which setsdress_declaration_open_brace_position,dress_statement_open_brace_position,dress_lambda_open_brace_positioninstead) - Applies to: Owned token boundaries.
- Guarantee: Only boundary whitespace changes.
- Shown with:
csharp_indent_braces = false
class Example { int Count { get { get { return 0; } }}Action action = delegate {Action action = delegate{ Work();};var point = new {var point = new{ X = 1};if (ready) {if (ready){ Work();}class Example { event Action Changed { event Action Changed { add => Work(); remove => Work(); }}class Example { int this[int index] { int this[int index] { get => index; }}Action action = () => {Action action = () =>{ Work();};void Local() {void Local(){ Work();}class Example { void Run() { void Run() { Work(); }}int[] numbers = new[] {int[] numbers = new[]{ 1, 2};class Example { int Count { int Count { get => 0; }}class Example {class Example{ int count;}New line before else
Section titled “New line before else”csharp_new_line_before_else = trueControls owned token boundaries. Only boundary whitespace changes.
- Values:
truefalse - Default:
true - Applies to: Owned token boundaries.
- Guarantee: Only boundary whitespace changes.
if (ready) { Work(); } else { Wait(); }if (ready) { Work(); } else { Wait(); }if (ready) { Work(); }else { Wait(); }Code unchanged.
New line before catch
Section titled “New line before catch”csharp_new_line_before_catch = trueControls owned token boundaries. Only boundary whitespace changes.
- Values:
truefalse - Default:
true - Applies to: Owned token boundaries.
- Guarantee: Only boundary whitespace changes.
try { Work(); } catch (Exception) { Recover(); } finally { CleanUp(); }try { Work(); } catch (Exception) { Recover(); } finally { CleanUp(); }try { Work(); }catch (Exception) { Recover(); } finally { CleanUp(); }Code unchanged.
New line before finally
Section titled “New line before finally”csharp_new_line_before_finally = trueControls owned token boundaries. Only boundary whitespace changes.
- Values:
truefalse - Default:
true - Applies to: Owned token boundaries.
- Guarantee: Only boundary whitespace changes.
try { Work(); } catch (Exception) { Recover(); } finally { CleanUp(); }try { Work(); } catch (Exception) { Recover(); } finally { CleanUp(); }try { Work(); } catch (Exception) { Recover(); }finally { CleanUp(); }Code unchanged.
New line before members in object initializers
Section titled “New line before members in object initializers”csharp_new_line_before_members_in_object_initializers = trueControls owned token boundaries. Only boundary whitespace changes.
- Values:
truefalse - Default:
true - Applies to: Owned token boundaries.
- Guarantee: Only boundary whitespace changes.
var value = new Example { First = 1, Second = 2 };var value = new Example { First = 1, Second = 2 };var value = new Example {First = 1,Second = 2};Code unchanged.
New line before members in anonymous types
Section titled “New line before members in anonymous types”csharp_new_line_before_members_in_anonymous_types = trueControls owned token boundaries. Only boundary whitespace changes.
- Values:
truefalse - Default:
true - Applies to: Owned token boundaries.
- Guarantee: Only boundary whitespace changes.
var value = new { First = 1, Second = 2 };var value = new { First = 1, Second = 2 };var value = new {First = 1,Second = 2};Code unchanged.
New line between query expression clauses
Section titled “New line between query expression clauses”csharp_new_line_between_query_expression_clauses = trueControls owned token boundaries. Only boundary whitespace changes.
- Values:
truefalse - Default:
true - Applies to: Owned token boundaries.
- Guarantee: Only boundary whitespace changes.
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 itemswhere item.Activeselect item.Name;Code unchanged.
Multiline parameter list open brace position
Section titled “Multiline parameter list open brace position”dress_multiline_parameter_list_open_brace_position = same_linePlaces 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_linenext_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.
class Example{ void Run( int value ) { }}Code unchanged.
class Example{ void Run( int value ) { ){ }}Preserve single line
Section titled “Preserve single line”Single-line blocks
Section titled “Single-line blocks”dress_single_line_blocks = expand_unless_trivialKeeps 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:
preserveexpandexpand_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.
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;}}Code unchanged.
class Example { bool Ready { get; } void Run() { } void Work() { Ready = true; } }class Example {bool Ready {get;} void Run() {} void Work() {Ready = true;}}Preserve single line blocks
Section titled “Preserve single line blocks”csharp_preserve_single_line_blocks = unsetControls 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:
truefalse - Default:
unset(left out bydotnet dress init, which setsdress_single_line_blocksinstead) - Applies to: Existing single-line blocks and accessor lists.
- Guarantee: False expands safe single-line blocks and accessor lists.
class Example { void Run() { Work(); } void Work() { } }Code unchanged.
class Example { void Run() { Work(); } void Work() { } }class Example {void Run() {Work();} void Work() {}}Preserve single line statements
Section titled “Preserve single line statements”csharp_preserve_single_line_statements = falseControls adjacent statements and member declarations. False separates safe adjacent statements and members.
- Values:
truefalse - Default:
false - Applies to: Adjacent statements and member declarations.
- Guarantee: False separates safe adjacent statements and members.
class Example { void Run() { Work(); } void Work() { } }class Example { void Run() { Work(); } void Work() { } }class Example { void Run() { Work(); }void Work() { } }Code unchanged.
Preferred modifier order
Section titled “Preferred modifier order”csharp_preferred_modifier_order = public,protected,internal,private,file,new,static,abstract,virtual,sealed,override,readonly,unsafe,required,volatile,asyncControls member modifier lists. Only modifier token order changes.
- Values:
publicprotectedinternalprivatefilenewstaticabstractvirtualsealedoverridereadonlyunsaferequiredvolatileasync— 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.
class Example { readonly public static int Value; }class Example { readonly public static int Value; }class Example { public static readonly int Value; }class Example { readonly public static int Value; }class Example { readonly static public int Value; }Namespace style
Section titled “Namespace style”dress_namespace_style = file_scopedControls 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_scopedblock_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.
namespace Example{ class Value { }}namespace Example{namespace Example; class Value { }}Code unchanged.
Body styles
Section titled “Body styles”Method body
Section titled “Method body”dress_method_body = unsetControls method bodies. The selected body form preserves the represented statement or returned expression.
- Values:
blockexpression - 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.
class Example { int Value() { return 1; } }Code unchanged.
class Example { int Value() { return 1; } }class Example { int Value() => 1; }Constructor body
Section titled “Constructor body”dress_constructor_body = unsetControls constructor bodies. The selected body form preserves the represented statement or returned expression.
- Values:
blockexpression - 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.
class Example { int value; public Example(int value) { this.value = value; } }Code unchanged.
class Example { int value; public Example(int value) { this.value = value; } }class Example { int value; public Example(int value) => this.value = value; }Operator body
Section titled “Operator body”dress_operator_body = unsetControls operator bodies. The selected body form preserves the represented statement or returned expression.
- Values:
blockexpression - 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.
class Example { public static Example operator +(Example a, Example b) { return a; } }Code unchanged.
class Example { public static Example operator +(Example a, Example b) { return a; } }class Example { public static Example operator +(Example a, Example b) => a; }Property body
Section titled “Property body”dress_property_body = expressionControls property bodies. The selected body form preserves the represented statement or returned expression.
- Values:
blockexpression - Default:
expression - Applies to: Property bodies.
- Guarantee: The selected body form preserves the represented statement or returned expression.
class Example { int Value { get { return 1; } } }class Example { int Value { get { return 1; } } }class Example { int Value => 1; }Code unchanged.
Indexer body
Section titled “Indexer body”dress_indexer_body = expressionControls indexer bodies. The selected body form preserves the represented statement or returned expression.
- Values:
blockexpression - Default:
expression - Applies to: Indexer bodies.
- Guarantee: The selected body form preserves the represented statement or returned expression.
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; }Code unchanged.
Accessor body
Section titled “Accessor body”dress_accessor_body = expressionControls accessor bodies. The selected body form preserves the represented statement or returned expression.
- Values:
blockexpression - Default:
expression - Applies to: Accessor bodies.
- Guarantee: The selected body form preserves the represented statement or returned expression.
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); } }Code unchanged.
Lambda body
Section titled “Lambda body”dress_lambda_body = expressionControls lambda bodies. A single return statement and its expression-bodied form represent the same expression.
- Values:
blockexpression - Default:
expression - Applies to: Lambda bodies.
- Guarantee: A single return statement and its expression-bodied form represent the same expression.
class Example{ Func<int> Value = () => { return 1; };}class Example{ Func<int> Value = () => { return 1; }; Func<int> Value = () => 1;}Code unchanged.
Embedded statements
Section titled “Embedded statements”Control statement body placement
Section titled “Control statement body placement”dress_embedded_statement_placement = next_linePlace 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_linenext_line - Default:
next_line - Applies to: The boundary before brace-optional embedded statements.
- Guarantee: Only boundary whitespace changes.
class Example{ void Run() { if (true) Work(); } void Work() { }}class Example{ void Run() { if (true) Work(); } void Run() { if (true) Work(); } void Work() { }}Code unchanged.
Embedded statement braces
Section titled “Embedded statement braces”dress_embedded_statement_braces = balancedControls brace-optional embedded statements. Brace changes preserve control flow and use planned body layout.
- Values:
compactbalancedalways - Default:
balanced - Applies to: Brace-optional embedded statements.
- Guarantee: Brace changes preserve control flow and use planned body layout.
class Example{ void Run() { if (true) Work(); } void Work() { }}Code unchanged.
Code unchanged.
class Example{ void Run() { if (true) Work(); } void Run() { if (true) { Work(); } } void Work() { }}Braces for multiline statement header
Section titled “Braces for multiline statement header”dress_braces_for_multiline_statement_header = trueControls multiline statement headers owning brace-optional embedded statements. A multiline statement header contributes only a brace constraint.
- Values:
truefalse - Default:
true - Applies to: Multiline statement headers owning brace-optional embedded statements.
- Guarantee: A multiline statement header contributes only a brace constraint.
if (firstCondition && secondCondition) Work();if (firstCondition && secondCondition) { Work(); }Code unchanged.