.NET Code Style Settings For Editorconfig

Possible values

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, suggestion, warning, or error). Severity means the level of enforcement for that style you want in your code base.

Severity effect
none Do not show anything to the user when this style is not being followed, however code generation features will 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 Code Style Options

"This." and "Me." Qualification

Fields

Option Name dotnet_style_qualification_for_field
Applicable Languages C# and Visual Basic
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 VisualBasic code style settings:
[*.cs,*.vb]
dotnet_style_qualification_for_field = false:suggestion

Properties

Option Name dotnet_style_qualification_for_property
Applicable Languages C# and Visual Basic
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 VisualBasic code style settings:
[*.cs,*.vb]
dotnet_style_qualification_for_property = false:suggestion

Methods

Option Name dotnet_style_qualification_for_method
Applicable Languages C# and Visual Basic
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 VisualBasic code style settings:
[*.cs, *.vb]
dotnet_style_qualification_for_method = false:suggestion

Events

Option Name dotnet_style_qualification_for_event
Applicable Languages C# and Visual Basic
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 VisualBasic 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

Option Name dotnet_style_predefined_type_for_locals_parameters_members
Applicable Languages C# and Visual Basic
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 VisualBasic code style settings:
[*.cs, *.vb]
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion

Member access expressions

Option Name dotnet_style_predefined_type_for_member_access
Applicable Languages C# and Visual Basic
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 VisualBasic code style settings:
[*.cs, *.vb]
dotnet_style_predefined_type_for_member_access = true:suggestion

Expression-level Preferences

Object initializers

Option Name dotnet_style_object_initializer
Applicable Languages C# and Visual Basic
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 VisualBasic code style settings:
[*.cs, *.vb]
dotnet_style_object_initializer = true:suggestion

Collection initializers

Option Name dotnet_style_collection_initializer
Applicable Languages C# and Visual Basic
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 VisualBasic code style settings:
[*.cs, *.vb]
dotnet_style_collection_initializer = true:suggestion

Explicit tuple names

Option Name dotnet_style_explicit_tuple_names
Applicable Languages C# and Visual Basic
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 VisualBasic code style settings:
[*.cs, *.vb]
dotnet_style_explicit_tuple_names = true:suggestion

Coalescing expressions in "null" checking

Option Name dotnet_style_coalesce_expression
Applicable Languages C# and Visual Basic
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 VisualBasic code style settings:
[*.cs, *.vb]
dotnet_style_coalesce_expression = true:suggestion

Null propagation in "null" checking

Option Name dotnet_style_null_propagation
Applicable Languages C# and Visual Basic
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 VisualBasic code style settings:
[*.cs, *.vb]
dotnet_style_null_propagation = true:suggestion

CSharp Code Style Settings

"var"

"var" for built-in types

Option Name csharp_style_var_for_built_in_types
Applicable Languages C#
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

Option Name csharp_style_var_when_type_is_apparent
Applicable Languages C#
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

Option Name csharp_style_var_elsewhere
Applicable Languages C#
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

Methods

Option Name csharp_style_expression_bodied_methods
Applicable Languages C#
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

Option Name csharp_style_expression_bodied_constructors
Applicable Languages C#
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

Option Name csharp_style_expression_bodied_operators
Applicable Languages C#
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

Option Name csharp_style_expression_bodied_properties
Applicable Languages C#
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 = false:none

Indexers

Option Name csharp_style_expression_bodied_indexers
Applicable Languages C#
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

Option Name csharp_style_expression_bodied_accessors
Applicable Languages C#
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

Option Name csharp_style_pattern_matching_over_is_with_cast_check
Applicable Languages C#
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

Option Name csharp_style_pattern_matching_over_as_with_null_check
Applicable Languages C#
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

Option Name csharp_style_inlined_variable_declaration
Applicable Languages C#
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

"Null" Checking Preferences

Throw-expressions

Option Name csharp_style_throw_expression
Applicable Languages C#
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

Option Name csharp_style_conditional_delegate_call
Applicable Languages C#
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 preform 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