.NET Coding Convention Settings For EditorConfig

.NET coding conventions are configured using an EditorConfig file. EditorConfig files allow you to enable/disable individual .NET coding conventions and configure the degree at which you want the convention enforced (via a severity level). To learn more about how to use EditorConfig to enforce consistency on your codebase, read this article.

There are three supported .NET coding convention categories:

  • Language Conventions are rules pertaining to the C# or Visual Basic language, for example, var/explicit type, use expression-bodied member.
  • Formatting Rules are rules regarding the layout and structure of your code in order to make it easier to read, for example, Allman braces, spaces in control blocks.
  • Naming Conventions are rules respecting the way objects are named, for example, async methods must end in "Async".

Language Conventions

Overview

Rule Format: options_name = false|true : none|suggestion|warning|error

For code style option, you must specify true (prefer this option) or false (do not prefer this option), a colon (:), and a severity (none, silent, suggestion, warning, or error). Severity means the level of enforcement for that style you want in your code base.

none and silent are synonymous and mean that no indication of any kind should be shown to the user. This has the effect of disabling this rule.

Severity effect
none/silent Do not show anything to the user when this style is not being followed, however code generation features generate in this style.
suggestion When this style is not being followed, show it to the user as a suggestion (underlying dots on the first two characters).
warning When this style is not being followed, show a compiler warning.
error When this style is not being followed, show a compiler error.

.NET Language Convention Options

"This." and "Me." Qualification

Fields (IDE0003/IDE0009)

Option Name Applicable Languages Visual Studio Default Supported Version
dotnet_style_qualification_for_field C# and Visual Basic false:none Visual Studio 2017 RTW
Value Description Applied
True Prefer all non-static fields used in non-static methods to be prefaced with this. in C# or Me. in Visual Basic. C#:
this.capacity = 0;

Visual Basic:
Me.capacity = 0
False Prefer all non-static fields used in non-static methods to not be prefaced with this. in C# or Me. in Visual Basic. C#:
capacity = 0;

Visual Basic:
capacity = 0

Example editorconfig file:

# CSharp and Visual Basic code style settings:
[*.{cs,vb}]
dotnet_style_qualification_for_field = false:suggestion

Properties (IDE0003/IDE0009)

Option Name Applicable Languages Visual Studio Default Supported Version
dotnet_style_qualification_for_property C# and Visual Basic false:none Visual Studio 2017 RTW
Value Description Applied
True Prefer the all non-static properties used in non-static methods to be prefaced with this. in C# or Me. in Visual Basic. C#:
this.ID = 0;

Visual Basic:
Me.ID = 0
False Prefer all non-static properties used in non-static methods to not be prefaced with this. in C# or Me. in Visual Basic. C#:
ID = 0;

Visual Basic:
ID = 0

Example editorconfig file:

# CSharp and Visual Basic code style settings:
[*.{cs,vb}]
dotnet_style_qualification_for_property = false:suggestion

Methods (IDE0003/IDE0009)

Option Name Applicable Languages Visual Studio Default Supported Version
dotnet_style_qualification_for_method C# and Visual Basic false:none Visual Studio 2017 RTW
Value Description Applied
True Prefer all non-static methods called from within non-static methods to be prefaced with this. in C# and Me. in VB. C#:
this.Display();

Visual Basic:
Me.Display()
False Prefer all non-static methods called from within non-static methods to not be prefaced with this.in C# and Me. in VB. C#:
Display();

Visual Basic:
Display()

Example editorconfig file:

# CSharp and Visual Basic code style settings:
[*.{cs,vb}]
dotnet_style_qualification_for_method = false:suggestion

Events (IDE0003/IDE0009)

Option Name Applicable Languages Visual Studio Default Supported Version
dotnet_style_qualification_for_event C# and Visual Basic false:none Visual Studio 2017 RTW
Value Description Applied
True Prefer all non-static events referenced from within non-static methods to be prefaced with this. in C# and Me. in VB. C#:
this.Elapsed += Handler;

Visual Basic:
AddHandler Me.Elapsed, AddressOf Handler
False Prefer all non-static events referenced from within non-static methods to not be prefaced with this.in C# and Me. in VB. C#:
Elapsed += Handler;

Visual Basic:
AddHandler Elapsed, AddressOf Handler

Example editorconfig file:

# CSharp and Visual Basic code style settings:
[*.{cs,vb}]
dotnet_style_qualification_for_event = false:suggestion

Language keywords (int, string, etc.) vs framework type names for type references

Locals, parameters, and members (IDE0012/IDE0014)

Option Name Applicable Languages Visual Studio Default Supported Version
dotnet_style_predefined_type_for_locals_parameters_members C# and Visual Basic true:none Visual Studio 2017 RTW
Value Description Applied
True For locals, parameters and type members, prefer types that have a language keyword to represent them (int, double, float, short, long, decimal, string) to use the keyword instead of the type name (Int32, Int64, etc.). C#:
private int _member;

Visual Basic: Private _member As Integer
False For locals, parameters and type members, prefer types that have a language keyword to represent them (int, double, float, short, long, decimal, string) to use the type name (Int32, Int64, etc.) instead of the keyword. C#:
private Int32 _member;

Visual Basic:
Private _member As Int32

Example editorconfig file:

# CSharp and Visual Basic code style settings:
[*.{cs,vb}]
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion

Member access expressions (IDE0013/IDE0015)

Option Name Applicable Languages Visual Studio Default Supported Version
dotnet_style_predefined_type_for_member_access C# and Visual Basic true:none Visual Studio 2017 RTW
Value Description Applied
True Prefer the keyword whenever a member-access expression is used on a type with a keyword representation (int, double, float, short, long, decimal, string). C#:
var local = int.MaxValue;

Visual Basic:
Dim local = Integer.MaxValue
False Prefer the type name whenever a member access expression is used on a type with a keyword representation (int, double, float, short, long, decimal, string). C#:
var local = Int32.MaxValue;

Visual Basic:
Dim local = Int32.MaxValue

Example editorconfig file:

# CSharp and Visual Basic code style settings:
[*.{cs,vb}]
dotnet_style_predefined_type_for_member_access = true:suggestion

Expression-level Preferences

Object initializers (IDE0017)

Option Name Applicable Languages Visual Studio Default Supported Version
dotnet_style_object_initializer C# and Visual Basic true:suggestion Visual Studio 2017 RTW
Value Description Applied
True Prefer objects to be initialized using object initializers when possible. C#:
var c = new Customer(){ Age = 21 };

Visual Basic:
Dim c = New Customer() With { .Age = 21 }
False Prefer objects to not be initialized using object initializers. C#:
var c = new Customer();
c.Age = 21;

Visual Basic:
Dim c = new Customer()
c.Age = 21

Example editorconfig file:

# CSharp and Visual Basic code style settings:
[*.{cs,vb}]
dotnet_style_object_initializer = true:suggestion

Collection initializers (IDE0028)

Option Name Applicable Languages Visual Studio Default Supported Version
dotnet_style_collection_initializer C# and Visual Basic true:suggestion Visual Studio 2017 RTW
Value Description Applied
True Prefer collections to be initialized using collection initializers when possible. C#:
var list = new List<int>{ 1, 2, 3 };

Visual Basic:
Dim list = new List(Of Integer) From { 1, 2, 3}
False Prefer objects to not be initialized using collection initializers. C#:
var list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);

Visual Basic:
Dim list = new List(Of Integer)
list.Add(1)
list.Add(2)
list.Add(3)

Example editorconfig file:

# CSharp and Visual Basic code style settings:
[*.{cs,vb}]
dotnet_style_collection_initializer = true:suggestion

Explicit tuple names (IDE0033)

Option Name Applicable Languages Visual Studio Default Supported Version
dotnet_style_explicit_tuple_names C# 7.0+ and Visual Basic 15+ true:suggestion Visual Studio 2017 RTW
Value Description Applied
True Prefer tuple names to ItemX properties. C#:
(string name, int age) customer = GetCustomer();
var name = customer.name;

Visual Basic:
Dim customer As (name As String, age As Integer) = GetCustomer()
Dim name = customer.name
False Prefer ItemX properties to tuple names. C#:
(string name, int age) customer = GetCustomer();
var name = customer.Item1;

Visual Basic:
Dim customer As (name As String, age As Integer) = GetCustomer()
Dim name = customer.Item1

Example editorconfig file:

# CSharp and Visual Basic code style settings:
[*.{cs,vb}]
dotnet_style_explicit_tuple_names = true:suggestion

Coalescing expressions in "null" checking (IDE0029)

Option Name Applicable Languages Visual Studio Default Supported Version
dotnet_style_coalesce_expression C# and Visual Basic true:suggestion Visual Studio 2017 RTW
Value Description Applied
True Prefer null coalescing expression to ternary operator checking. C#:
var v = x ?? y;

Visual Basic:
Dim v = If(x, y)
False Prefer ternary operator checking to null coalescing expression. C#:
var v = x != null ? x : y; // or
var v = x == null ? y : x;

Visual Basic:
Dim v = If(x Is Nothing, y, x) ' or
Dim v = If(x IsNot Nothing, x, y)

Example editorconfig file:

# CSharp and Visual Basic code style settings:
[*.{cs,vb}]
dotnet_style_coalesce_expression = true:suggestion

Null propagation in "null" checking (IDE0031)

Option Name Applicable Languages Visual Studio Default Supported Version
dotnet_style_null_propagation C# 6.0+ and Visual Basic 14+ true:suggestion Visual Studio 2017 RTW
Value Description Applied
True Prefer to use null-conditional operator where possible. C#:
var v = o?.ToString();

Visual Basic:
Dim v = o?.ToString()
False Prefer to use ternary null checking where possible. C#:
var v = o == null ? null : o.ToString(); // or
var v = o != null ? o.String() : null;

Visual Basic:
Dim v = If(o Is Nothing, Nothing, o.ToString()) ' or
Dim v = If(o IsNot Nothing, o.ToString(), Nothing)

Example editorconfig file:

# CSharp and Visual Basic code style settings:
[*.{cs,vb}]
dotnet_style_null_propagation = true:suggestion

CSharp Code Style Settings

"var" and Explicit Types

"var" for built-in types (IDE0007, IDE0008)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_var_for_built_in_types C# true:none Visual Studio 2017 RTW
Value Description Applied
True Prefer var is used for built-in system types such as int. C#:
var x = 5;
False Prefer var not be used for built-in system types such as int. C#:
int x = 5;

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_var_for_built_in_types = true:suggestion

"var" when type is apparent (IDE0007, IDE0008)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_var_when_type_is_apparent C# true:none Visual Studio 2017 RTW
Value Description Applied
True Prefer var when the type is already mentioned on the right-hand side of a declaration expression. C#:
var obj = new C();
False Prefer to not use var when the type is already mentioned on the right-hand side of a declaration expression. C#:
C obj = new C();

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_var_when_type_is_apparent = true:suggestion

"var" elsewhere (IDE0007, IDE0008)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_var_elsewhere C# true:none Visual Studio 2017 RTW
Value Description Applied
True Prefer var in all cases unless overridden by another code style rule. C#:
var f = this.Init();
False Prefer to not use var in all cases unless overridden by another code style rule. C#:
bool f = this.Init();

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_var_elsewhere = true:suggestion

Expression-bodied Members

Methods (IDE0022)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_expression_bodied_methods C# 6.0+ false:none Visual Studio 2017 RTW
Value Description Applied
True Prefer expression-bodied members for methods. C#:
public int GetAge() => this.Age;
False Do not prefer expression-bodied members for methods. C#:
public int GetAge() { return this.Age; }

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_expression_bodied_methods = false:none

Constructors (IDE0021)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_expression_bodied_constructors C# 7.0+ false:none Visual Studio 2017 RTW
Value Description Applied
True Prefer expression-bodied members for constructors. C#:
public Customer(int age) => Age = age;
False Do not prefer expression-bodied members for constructors. C#:
public Customer(int age) { Age = age; }

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_expression_bodied_constructors = false:none

Operators (IDE0023, IDE0024)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_expression_bodied_operators C# 7.0+ false:none Visual Studio 2017 RTW
Value Description Applied
True Prefer expression-bodied members for operators. C#:
public static ComplexNumber operator +(ComplexNumber c1, ComplexNumber c2)
=> new ComplexNumber(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
False Do not prefer expression-bodied members for operators. C#:
public static ComplexNumber operator +(ComplexNumber c1, ComplexNumber c2)
{ return new ComplexNumber(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary); }

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_expression_bodied_operators = false:none

Properties (IDE0025)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_expression_bodied_properties C# 7.0+ true:none Visual Studio 2017 RTW
Value Description Applied
True Prefer expression-bodied members for properties. C#:
public int Age => _age;
False Do not prefer expression-bodied members for properties. C#:
public int Age { get { return _age; }}

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_expression_bodied_properties = true:none

Indexers (IDE0026)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_expression_bodied_indexers C# 7.0+ true:none Visual Studio 2017 RTW
Value Description Applied
True Prefer expression-bodied members for indexers. C#:
public T this[int i] => _value[i];
False Do not prefer expression-bodied members for indexers. C#:
public T this[int i] { get { return _values[i]; } }

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_expression_bodied_indexers = false:none

Accessors (IDE0027)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_expression_bodied_accessors C# 7.0+ true:none Visual Studio 2017 RTW
Value Description Applied
True Prefer expression-bodied members for accessors. C#:
public int Age { get => _age; set => _age = value; }
False Do not prefer expression-bodied members for accessors. C#:
public int Age { get { return _age; } set { _age = value; } }

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_expression_bodied_accessors = false:none

Pattern matching

"is" with "cast" checking (IDE0020)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_pattern_matching_over_is_with_cast_check C# 7.0+ true:suggestion Visual Studio 2017 RTW
Value Description Applied
True Prefer pattern matching instead of is expressions with type casts. C#:
if (o is int i) {...}
False Prefer is expressions with type casts instead of pattern matching. C#:
if (o is int) {var i = (int)o; ... }

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion

"as" with "null" checking (IDE0019)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_pattern_matching_over_as_with_null_check C# 7.0+ true:suggestion Visual Studio 2017 RTW
Value Description Applied
True Prefer pattern matching instead of as expressions with null checks to determine if something is of a particular type. C#:
if (o is string s) {...}
False Prefer as expressions with null checks instead of pattern matching to determine if something is of a particular type. C#:
var s = o as string; if (s != null) {...}

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion

Inlined variable declarations (IDE0018)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_inlined_variable_declaration C# 7.0+ true:suggestion Visual Studio 2017 RTW
Value Description Applied
True Prefer out variables to be declared inline when possible. C#:
if (int.TryParse(value, out int i) {...}
False Prefer out variables to be declared explicitly. C#:
int i; if (int.TryParse(value, out i) {...}

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_inlined_variable_declaration = true:suggestion

Expression-level Preferences

Simplify default expressions (IDE0034)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_prefer_simple_default_expression C# 7.1+ true:suggestion Visual Studio 2017 v. 15.3
Value Description Applied
True Prefer default over default(T) C#:
void DoWork(CancellationToken cancellationToken = default){ ... }
False Prefer. C#:
void DoWork(CancellationToken cancellationToken = default(CancellationToken)){ ... }

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_prefer_simple_default_expression = true:suggestion

"Null" Checking Preferences

Throw-expressions (IDE0016)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_throw_expression C# 7.0+ true:suggestion Visual Studio 2017 RTW
Value Description Applied
True Prefer to use throw expressions instead of throw statements. C#:
this.s = ss ?? throw new ArgumentNullException(nameof(s));
False Prefer to use throw statements instead of throw expressions. C#:
if (s==null) {throw new ArgumentNullException(nameof(s));} this.s = s;

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_throw_expression = true:suggestion

Prefer conditional delegate calls (IDE0041)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_style_conditional_delegate_call C# 6.0+ true:suggestion Visual Studio 2017 RTW
Value Description Applied
True Prefer to use conditional coalescing operation (?.) when invoking a lambda instead of performing a null check. C#:
func?.Invoke(args);
False Prefer to perform a null check before invoking a lambda instead of using the conditional coalescing operator (?.). C#:
if (func!=null) { func(args); }

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_style_conditional_delegate_call = false:suggestion

"Code Block Preferences

Prefer braces (IDE0011)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_prefer_braces C# true:none Visual Studio 2017 v. 15.3
Value Description Applied
True Prefer braces C#:
if (test) { this.Display(); }
False Prefer no braces when possible C#:
if (test) this.Display();

Example editorconfig file:

# CSharp code style settings:
[*.cs]
csharp_prefer_braces = true:none

Formatting Rules

Overview

Rule Format: options_name = false|true

For formatting options, you must specify true (prefer this option) or false (do not prefer this option) except in a couple cases where you must instead specify what conditions you want the rule applied to.

.NET Formatting Options

Organize Usings

Sort System Directives First

Option Name Applicable Languages Visual Studio Default Supported Version
dotnet_sort_system_directives_first C# and Visual Basic true Visual Studio 2017 v. 15.3
Value Description Applied
True Sort System.* usings alphabetically and place them before other usings. C#:
using System.Collections.Generic;
using System.Threading.Tasks;
using Octokit;
False Have no requirements on the ordering of usings C#:
using System.Collections.Generic;
using Octokit;
using System.Threading.Tasks;

Example editorconfig file:

# .NET formatting settings:
[*.{cs,vb}]
dotnet_sort_system_directives_first = true

C# Formatting Settings

Newline Options

Newline Before Open Brace ({)

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_new_line_before_open_brace C# true Visual Studio 2017 v. 15.3
Value Description
accessors, anonymous_methods, anonymous_types, control_blocks, events, indexers, lambdas, local_functions, methods, object_collection, properties, types. (For multiple, separate with ','). Require braces to be on a new line for the given expressions (Allman style)
all Require braces to be on a new line for all expressions (Allman)
none Require braces to be on the same line for all expressions (K&R)

Applied:

// csharp_new_line_before_open_brace = all
void MyMethod() 
{
    if (...) 
    {
        ...
    }
}
// csharp_new_line_before_open_brace = none
void MyMethod() {
    if (...) {
        ...
    }
}

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_new_line_before_open_brace = methods, properties, control_blocks, types

Newline Before else

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_new_line_before_else C# true Visual Studio 2017 v. 15.3
Value Description
True Place else statements on a new line.
False Place else statements on the same line.

Applied:

// csharp_new_line_before_else = true
if (...) {
    ...
}
else {
    ...
}
// csharp_new_line_before_else = false
if (...) {
    ...
} else {
    ...
}

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_new_line_before_else = true

Newline Before catch

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_new_line_before_catch C# true Visual Studio 2017 v. 15.3
Value Description
True Place catch statements on a new line.
False Place catch statements on the same line.

Applied:

// csharp_new_line_before_catch = true
try {
    ...
}
catch (Exception e) {
    ...
}
// csharp_new_line_before_catch = false
try {
    ...
} catch (Exception e) {
    ...
}

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_new_line_before_catch = true

Newline Before finally

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_new_line_before_finally C# true Visual Studio 2017 v. 15.3
Value Description
True Require finally statements to be on a new line after the closing brace.
False Require finally statements to be on the same line as the closing brace.

Applied:

// csharp_new_line_before_finally = true
try {
    ...
}
catch (Exception e) {
    ...
}
finally {
    ...
}
// csharp_new_line_before_finally = false
try {
    ...
} catch (Exception e) {
    ...
} finally {
    ...
}

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_new_line_before_finally = true

Newline Before Members in Object Initializers

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_new_line_before_members_in_object_initializers C# true Visual Studio 2017 v. 15.3
Value Description
True Require members of object intializers to be on separate lines.
False Require members of object initializers to be on the same line.

Applied:

// csharp_new_line_before_members_in_object_initializers = true
var z = new B()
{
    A = 3,
    B = 4
}
// csharp_new_line_before_members_in_object_initializers = false
var z = new B()
{
    A = 3, B = 4
}

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_new_line_before_members_in_object_initializers = true

Newline Before Members in Anonymous Types

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_new_line_before_members_in_anonymous_types C# true Visual Studio 2017 v. 15.3
Value Description
True Require members of anonymous types to be on separate lines.
False Require members of anonymous types to be on the same line.

Applied:

// csharp_new_line_before_members_in_anonymous_types = true
var z = new
{
    A = 3,
    B = 4
}
// csharp_new_line_before_members_in_anonymous_types = false
var z = new
{
    A = 3, B = 4
}

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_new_line_before_members_in_anonymous_types = true

Newline Before Members in Query Expression Clauses

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_new_line_within_query_expression_clauses C# true Visual Studio 2017 v. 15.3
Value Description
True Require elements of query expression clauses to be on separate lines.
False Require elements of query expression clauses to be on the same line.

Applied:

// csharp_new_line_within_query_expression_clauses = true
var q = from a in e
        from b in e
        select a * b;
// csharp_new_line_within_query_expression_clauses = false
var q = from a in e from b in e
        select a * b;

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_new_line_within_query_expression_clauses = true

Indentation Options

Indent switch Case Contents

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_indent_case_contents C# true Visual Studio 2017 v. 15.3
Value Description
True Indent switch case contents
False Do not indent switch case contents

Applied:

// csharp_indent_case_contents = true
switch(c) {
    case Color.Red:
        Console.WriteLine("The color is red");
        break;
    case Color.Blue:
        Console.WriteLine("The color is blue");
        break;
    default:
        Console.WriteLine("The color is unknown.");
        break;
}
// csharp_indent_case_contents = false
switch(c) {
    case Color.Red:
    Console.WriteLine("The color is red");
    break;
    case Color.Blue:
    Console.WriteLine("The color is blue");
    break;
    default:
    Console.WriteLine("The color is unknown.");
    break;
}

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_indent_case_contents = true

Indent switch Labels

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_indent_switch_labels C# true Visual Studio 2017 v. 15.3
Value Description
True Indent switch labels
False Do not indent switch labels

Applied:

// csharp_indent_switch_labels = true
switch(c) {
    case Color.Red:
        Console.WriteLine("The color is red");
        break;
    case Color.Blue:
        Console.WriteLine("The color is blue");
        break;
    default:
        Console.WriteLine("The color is unknown.");
        break;
}
// csharp_indent_switch_labels = false
switch(c) {
case Color.Red:
    Console.WriteLine("The color is red");
    break;
case Color.Blue:
    Console.WriteLine("The color is blue");
    break;
default:
    Console.WriteLine("The color is unknown.");
    break;
}

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_indent_switch_labels = true

Label Positioning

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_indent_labels C# one_less Visual Studio 2017 v. 15.3
Value Description
one_less Labels are placed at one less indent to the current context
no_change Labels are placed at the same indent as the current context

Applied:

// csharp_indent_labels = one_less
private string MyMethod(...) 
{
    if (...) {
        goto error;
    }
error:
    throw new Exception(...);
}
// csharp_indent_labels= no_change
private string MyMethod(...) 
{
    if (...) {
        goto error;
    }
    error:
    throw new Exception(...);
}

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_indent_labels = one_less

Spacing Options

Space After Cast

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_space_after_cast C# false Visual Studio 2017 v. 15.3
Value Description Applied
True Require a space between a cast and the value C#:
int y = (int) x;
False Require no space between the cast and the value C#:
int y = (int)x;

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_space_after_cast = true

Space After Keywords in Control Flow Statements

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_space_after_keywords_in_control_flow_statements C# true Visual Studio 2017 v. 15.3
Value Description Applied
True Require a space after a keyword C#:
for (int i;i<x;i++) { ... }
False Require no space after a keyword C#:
for(int i;i<x;i++) { ... }

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_space_after_keywords_in_control_flow_statements = true

Space Between Method Declaration Argument-List Parentheses

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_space_between_method_declaration_parameter_list_parentheses C# false Visual Studio 2017 v. 15.3
Value Description Applied
True Require a space after a keyword C#:
void Bark( int x ) { ... }
False Require no space after a keyword C#:
void Bark(int x) { ... }

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_space_between_method_declaration_parameter_list_parentheses = true

Space Within Parentheses for Method Call Argument List

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_space_between_method_call_parameter_list_parentheses C# false Visual Studio 2017 v. 15.3
Value Description Applied
true Place space between parentheses of control flow statements C#:
MyMethod( argument );
false Place space between parentheses of expressions C#:
MyMethod(argument);

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_space_between_method_call_parameter_list_parentheses = control_flow_statements, type_casts

Space Within Parentheses for Other Options

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_space_between_parentheses C# false Visual Studio 2017 v. 15.3
Value Description Applied
control_flow_statements Place space between parentheses of control flow statements C#:
for( int i;i<x;i++ ) { ... }
expressions Place space between parentheses of expressions C#:
var z = ( x * y ) - ( ( y - x ) * 3);
type_casts Place space between parentheses in type casts C#:
int y = ( int )x;

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_space_between_parentheses = control_flow_statements, type_casts

Wrapping Options

Leave Statements and Member Declarations on the Same Line

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_preserve_single_line_statements C# true Visual Studio 2017 v. 15.3
Value Description
True Leave statements and member declarations on the same line
False Leave statements and member declarations on different lines

Applied

//csharp_preserve_single_line_statements = true
int i = 0; string name = "John";
//csharp_preserve_single_line_statements = false
int i = 0; 
string name = "John";

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_preserve_single_line_statements = true

Leave Block on Single Line

Option Name Applicable Languages Visual Studio Default Supported Version
csharp_preserve_single_line_blocks C# true Visual Studio 2017 v. 15.3
Value Description
True Leave block on single line
False Leave block on separate lines

Applied

//csharp_preserve_single_line_blocks = true
public int Foo { get; set; }
//csharp_preserve_single_line_blocks = false
public int MyProperty
{ 
    get; set;
}

Example editorconfig file:

# CSharp formatting settings:
[*.cs]
csharp_preserve_single_line_blocks = true

Naming Conventions

Overview

Rule Format:
namingRuleTitle:
dotnet_naming_rule.<namingRuleTitle>.symbols = <symbolTitle>
dotnet_naming_rule.<namingRuleTitle>.style = <styleTitle>
dotnet_naming_rule.<namingRuleTitle>.severity = none|suggestion|warning|error

symbolTitle:
dotnet_naming_symbols.<symbolTitle>.applicable_kinds = class, struct, interface, enum, property, method, field, event, namespace, delegate, type_parameter
dotnet_naming_symbols.<symbolTitle>.applicable_accessibilities = public, internal, private, protected, protected_internal
dotnet_naming_symbols.<symbolTitle>.required_modifiers = abstract, async, const, readonly, static

styleTitle:
dotnet_naming_style.<styleTitle>.capitalization = pascal_case|camel_case|first_word_upper|all_upper|all_lower
dotnet_naming_style.<styleTitle>.required_prefix = string
dotnet_naming_style.<styleTitle>.required_suffix = string
dotnet_naming_style.<styleTitle>.word_separator = string

Writing a Naming Convention

For naming conventions, you must specify symbols, style, and a severity. Naming conventions should be ordered from most-specific to least-specific. The first rule encountered that can be applied, is the only rule applied.

Severity

The following are valid options for the severity of a naming style rule none, silent, suggestion, warning, error.

none and silent are synonymous and mean that no indication of any kind should be shown to the user. This has the effect of disabling this rule.

suggestion means that the user is shown the following in the Error List: and the following in the IDE. The suggestion severity allows the naming rule to run, but it doesn't cause the build to break.

Severity effect
none/silent Do not show anything to the user when this style is not being followed, however code generation features generate in this style.
suggestion When this style is not being followed, show it to the user as a suggestion (underlying dots on the first two characters).
warning When this style is not being followed, show a compiler warning.
error When this style is not being followed, show a compiler error.

Symbol Specification

Identify what symbols with which modifiers and at what accessibility level the naming rule should apply to.

Option Name dotnet_naming_rule.<namingRuleTitle>.symbols
dotnet_naming_symbols.<symbolTitle>.applicable_kinds
dotnet_naming_symbols.<symbolTitle>.applicable_accessibilities
dotnet_naming_symbols.<symbolTitle>.required_modifiers
Symbol Accessibility Modifiers
* * abstract
class public must_inherit
struct internal (C#) / const
interface friend (Visual Basic) readonly
enum private static
property protected shared
method protected_internal (C#)
field protected_friend (Visual Basic)
event
delegate

Style Specification

Identify the naming style to apply to the symbols.

Option Name dotnet_naming_rule.<namingRuleTitle>.style = <styleTitle>
dotnet_naming_style.<styleTitle>.capitalization
dotnet_naming_style.<styleTitle>.required_prefix
dotnet_naming_style.<styleTitle>.required_suffix
dotnet_naming_style.<styleTitle>.word_separator
Style Description
Prefix Required characters that must appear before the identifier.
Suffix Required characters that must appear after the identifier.
Word Separator Required separator between words in the identifier.
Capitalization pascal_case, camel_case, first_word_upper, all_upper, all_lower

Example Naming Convention

# Dotnet Naming Conventions
[*.{cs,vb}] 
dotnet_naming_rule.async_methods_end_in_async.symbols  = any_async_methods
dotnet_naming_rule.async_methods_end_in_async.style    = end_in_async
dotnet_naming_rule.async_methods_end_in_async.severity = suggestion

dotnet_naming_symbols.any_async_methods.applicable_kinds           = method
dotnet_naming_symbols.any_async_methods.applicable_accessibilities = *
dotnet_naming_symbols.any_async_methods.required_modifiers         = async

dotnet_naming_style.end_in_async.required_suffix = Async
dotnet_naming_style.end_in_async.capitalization  = pascal_case