The pattern book
Braces and bodies
Newlines
Section titled “Newlines”New line before open brace
Section titled “New line before open brace”csharp_new_line_before_open_brace = accessors,anonymous_methods,anonymous_types,control_blocks,events,indexers,lambdas,local_functions,methods,properties,typesControls owned token boundaries. Only boundary whitespace changes.
- Values:
accessorsanonymous_methodsanonymous_typescontrol_blockseventsindexerslambdaslocal_functionsmethodsobject_collection_array_initializerspropertiestypesallnone— 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; } }}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”Preserve single line blocks
Section titled “Preserve single line blocks”csharp_preserve_single_line_blocks = falseControls existing single-line blocks and accessor lists. False expands safe single-line blocks and accessor lists.
- Values:
truefalse - Default:
false - 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() { } }class Example { void Run() { Work(); } void Work() { } }class Example {void Run() {Work();} void Work() {}}Code unchanged.
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.
Preserve trivial single line blocks
Section titled “Preserve trivial single line blocks”dress_preserve_trivial_single_line_blocks = trueControls 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:
truefalse - 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
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;}}class Example { bool Ready { get; } void Run() { } void Work() { Ready = true; } }class Example {bool Ready {get;} void Run() {} void Work() {Ready = true;}}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 compilation-unit namespace declaration. 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 or expression statement and its expression-bodied form represent the same expression.
- Values:
blockexpression - Default:
expression - Applies to: Lambda bodies.
- Guarantee: A single return or expression 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.