C# Styling Rules for Apache.Avro

The following rules are currently used within the .editorconfig of the Avro solution. Any changes to this documentation should be reflected in the .editorconfig file and vice versa.

Notes

  • The examples shown are based on the current settings in .editorconfig
  • :exclamation: Not defined :exclamation: means we have not set a preference
  • There are cases where it is not explicitly defined in the .editorconfig, but there is a default option
  • The project currently targets a framework that uses C# 7.3
  • When violating a formatting rule it may show up as an IDE0055 Fix formatting violation.

New line preferences

csharp_new_line_before_open_brace

Reference

This rule concerns whether an open brace { should be placed on the same line as the preceding code, or on a new line.

Example

void MyMethod()
{
    if (...)
    {
        ...
    }
}

csharp_new_line_before_else

Reference

Example

if (...) {
    ...
}
else {
    ...
}

csharp_new_line_before_catch

Reference

Example

try {
    ...
}
catch (Exception e) {
    ...
}

csharp_new_line_before_finally

Reference

Example

try {
    ...
}
catch (Exception e) {
    ...
}
finally {
    ...
}

csharp_new_line_before_members_in_object_initializers

Reference

Example

var z = new B()
{
    A = 3,
    B = 4
}

csharp_new_line_before_members_in_anonymous_types

Reference

Example

var z = new
{
    A = 3,
    B = 4
}

csharp_new_line_between_query_expression_clauses

Reference

Example

var q = from a in e
        from b in e
        select a * b;

Indentation preferences

csharp_indent_case_contents

Reference

Example

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

Reference

Example

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_labels

Reference

Labels are placed at one less indent to the current context

Example

class C
{
    private string MyMethod(...)
    {
        if (...) {
            goto error;
        }
    error:
        throw new Exception(...);
    }
}

csharp_indent_block_contents

Reference

Example

static void Hello()
{
    Console.WriteLine("Hello");
}

csharp_indent_braces

Reference

Example

static void Hello()
{
    Console.WriteLine("Hello");
}

csharp_indent_case_contents_when_block

Reference

Example

case 0:
    {
        Console.WriteLine("Hello");
        break;
    }

Spacing Preferences

csharp_space_after_cast

Reference

Example

int y = (int)x;

csharp_space_after_keywords_in_control_flow_statements

Reference

Example

for (int i;i<x;i++) { ... }

csharp_space_between_parentheses

Reference

No additional spacing between parentheses

Example

for (int i;i<x;i++) { ... }

csharp_space_before_colon_in_inheritance_clause

Reference

Example

interface I
{

}

class C : I
{

}

csharp_space_after_colon_in_inheritance_clause

Reference

Example

interface I
{

}

class C : I
{

}

csharp_space_around_binary_operators

Reference

Insert space before and after the binary operator

Example

return x * (x - y);

csharp_space_between_method_declaration_parameter_list_parentheses

Reference

Remove space characters after the opening parenthesis and before the closing parenthesis of a method declaration parameter list

Example

void Bark(int x) { ... }

csharp_space_between_method_declaration_empty_parameter_list_parentheses

Reference

Remove space within empty parameter list parentheses for a method declaration

Example

void Goo()
{
    Goo(1);
}

void Goo(int x)
{
    Goo();
}

csharp_space_between_method_declaration_name_and_open_parenthesis

Reference

Remove space characters between the method name and opening parenthesis in the method declaration

Example

void M() { }

csharp_space_between_method_call_parameter_list_parentheses

Reference

Remove space characters after the opening parenthesis and before the closing parenthesis of a method call

Example

MyMethod(argument);

csharp_space_between_method_call_empty_parameter_list_parentheses

Reference

Remove space within empty argument list parentheses

Example

void Goo()
{
    Goo(1);
}

void Goo(int x)
{
    Goo();
}

csharp_space_between_method_call_name_and_opening_parenthesis

Reference

Remove space between method call name and opening parenthesis

Example

void Goo()
{
    Goo(1);
}

void Goo(int x)
{
    Goo();
}

csharp_space_after_comma

Reference

Insert space before a comma

Example

int[] x = new int[] { 1 , 2 , 3 , 4 , 5 };

csharp_space_after_dot

Reference

Remove space before a dot

Example

this.Goo();

csharp_space_after_semicolon_in_for_statement

Reference

Insert space after each semicolon in a for statement

Example

for (int i = 0; i < x.Length; i++)

csharp_space_before_semicolon_in_for_statement

Reference

Remove space before each semicolon in a for statement

Example

for (int i = 0; i < x.Length; i++)

csharp_space_around_declaration_statements

Reference

Remove extra space characters in declaration statements

Example

int x = 0;

csharp_space_before_open_square_brackets

Reference

Remove space before opening square brackets [

Example

int[] numbers = new int[] { 1, 2, 3, 4, 5 };

csharp_space_between_empty_square_brackets

Reference

Remove space between empty square brackets []

Example

int[] numbers = new int[] { 1, 2, 3, 4, 5 };

csharp_space_between_square_brackets

Reference

Remove space characters in non-empty square brackets [0]

Example

int index = numbers[0];

Wrap Preferences

csharp_preserve_single_line_statements

Reference

Leave statements and member declarations on different lines

Example

int i = 0;
string name = "John";

csharp_preserve_single_line_blocks

Reference

Leave code block on single line

Example

public int Foo { get; set; }

Using Directive Preferences

csharp_using_directive_placement

Reference

Prefer using directives to be placed outside the namespace

Example

using System;

namespace Conventions
{

}

Namespace Preferences

csharp_style_namespace_declarations

Reference

:exclamation: Not defined :exclamation:

Example

// csharp_style_namespace_declarations = block_scoped
using System;

namespace Convention
{
    class C
    {
    }
}

// csharp_style_namespace_declarations = file_scoped
using System;

namespace Convention;
class C
{
}

this and Me preferences

dotnet_style_qualification_for_field

Reference

Prefer fields not to be prefaced with this. or Me.

Example

capacity = 0;

dotnet_style_qualification_for_property

Reference

Prefer properties not to be prefaced with this. or Me.

Example

ID = 0;

dotnet_style_qualification_for_method

Reference

Prefer methods not to be prefaced with this. or Me.

Example

Display();

dotnet_style_qualification_for_event

Reference

Prefer events not to be prefaced with this. or Me.

Example

Elapsed += Handler;

Use language keywords instead of framework type names for type references

dotnet_style_predefined_type_for_locals_parameters_members

Reference

Prefer the language keyword for local variables, method parameters, and class members, instead of the type name, for types that have a keyword to represent them

Example

private int _member;

Order modifiers

csharp_preferred_modifier_order

Reference

public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion

Example

class MyClass
{
    private static readonly int _daysInYear = 365;
}

dotnet_style_require_accessibility_modifiers

Reference

Prefer accessibility modifiers to be declared except for public interface members.

default is for_non_interface_members

Example

class MyClass
{
    private const string thisFieldIsConst = "constant";
}

dotnet_style_readonly_field

Reference

Prefer that private fields should be marked with readonly (C#) or ReadOnly (Visual Basic) if they are only ever assigned inline, or inside of a constructor

Example

class MyClass
{
    private readonly int _daysInYear = 365;
}

csharp_prefer_static_local_function

Reference

Prefer local functions to be marked static

Example

void M()
{
    Hello();
    static void Hello()
    {
        Console.WriteLine("Hello");
    }
}

Parentheses Preferences

dotnet_style_parentheses_in_arithmetic_binary_operators

Reference

Prefer parentheses to clarify arithmetic operator (*, /, %, +, -, <<, >>, &, ^, |) precedence

Default is always_for_clarity

Example

var v = a + (b * c);

dotnet_style_parentheses_in_relational_binary_operators

Reference

Prefer parentheses to clarify relational operator (>, <, <=, >=, is, as, ==, !=) precedence

Default is always_for_clarity

Example

var v = (a < b) == (c > d);

dotnet_style_parentheses_in_other_binary_operators

Reference

Prefer parentheses to clarify other binary operator (&&, ||, ??) precedence

Default is always_for_clarity

Example

var v = a || (b && c);

dotnet_style_parentheses_in_other_operators

Reference

Prefer to not have parentheses when operator precedence is obvious

Default is never_if_unnecessary

Example

var v = a.b.Length;

Expression-level preferences

dotnet_style_object_initializer

Reference

Prefer objects to be initialized using object initializers when possible

default is true

Example

var c = new Customer() { Age = 21 };

csharp_style_inlined_variable_declaration

Reference

Prefer out variables to be declared inline in the argument list of a method call when possible

Example

if (int.TryParse(value, out int i) {...}

dotnet_style_collection_initializer

Reference

Prefer collections to be initialized using collection initializers when possible

Example

var list = new List<int> { 1, 2, 3 };

dotnet_style_prefer_auto_properties

Reference

Prefer auto properties over properties with private backing fields

Example

private int Age { get; }

dotnet_style_explicit_tuple_names

Reference

Prefer tuple names to ItemX properties

Example

(string name, int age) customer = GetCustomer();
var name = customer.name;

csharp_prefer_simple_default_expression

Reference

Prefer default over default(T)

Example

void DoWork(CancellationToken cancellationToken = default) { ... }

dotnet_style_prefer_inferred_tuple_names

Reference

Prefer inferred tuple element names

Example

var tuple = (age, name);

dotnet_style_prefer_inferred_anonymous_type_member_names

Reference

Prefer inferred anonymous type member names

Example

var anon = new { age, name };

csharp_style_pattern_local_over_anonymous_function

Reference

Prefer anonymous functions over local functions

Example

Func<int, int> fibonacci = null;
fibonacci = (int n) =>
{
    return n <= 1 ? 1 : fibonacci(n - 1) + fibonacci(n - 2);
};

csharp_style_deconstructed_variable_declaration

Reference

Prefer deconstructed variable declaration

default is true

Example

var (name, age) = GetPersonTuple();
Console.WriteLine($"{name} {age}");

(int x, int y) = GetPointTuple();
Console.WriteLine($"{x} {y}");

dotnet_style_prefer_conditional_expression_over_assignment

Reference

Prefer assignments with a ternary conditional over an if-else statement

Example

string s = expr ? "hello" : "world";

dotnet_style_prefer_conditional_expression_over_return

Reference

Prefer return statements to use a ternary conditional over an if-else statement

Example

return expr ? "hello" : "world"

dotnet_style_prefer_compound_assignment

Reference

Prefer compound assignment expressions

default is true

Example

x += 1;

dotnet_style_prefer_simplified_boolean_expressions

Reference

Prefer simplified conditional expressions

default is true

Example

var result1 = M1() && M2();
var result2 = M1() || M2();

csharp_style_implicit_object_creation_when_type_is_apparent

Reference

Prefer target-typed new expressions when created type is apparent

default is true

Example

C c = new();
C c2 = new() { Field = 0 };

Null-checking Preferences

csharp_style_throw_expression

Reference

Prefer to use throw expressions instead of throw statements

Example

_s = s ?? throw new ArgumentNullException(nameof(s));

dotnet_style_coalesce_expression

Reference

Prefer null coalescing expressions to ternary operator checking

Example

var v = x ?? y;

dotnet_style_coalesce_expression

Reference

Prefer to use null-conditional operator when possible

Example

string v = o?.ToString();

dotnet_style_prefer_is_null_check_over_reference_equality_method

Reference

Prefer is null check over reference equality method

Example

if (value is null)
    return;

csharp_style_conditional_delegate_call

Reference

Prefer to use the conditional coalescing operator (?.) when invoking a lambda expression, instead of performing a null check

Example

func?.Invoke(args);

var Preferences

csharp_style_var_for_built_in_types

Reference

Prefer explicit type over var to declare variables with built-in system types such as int

Example

int x = 5;

csharp_style_var_when_type_is_apparent

Reference

Prefer explicit type over var when the type is already mentioned on the right-hand side of a declaration expression

Example

Customer obj = new Customer();

csharp_style_var_elsewhere

Reference

Prefer explicit type over var in all cases, unless overridden by another code style rule

Example

bool f = this.Init();

Expression-bodied member Preferences

csharp_style_expression_bodied_constructors

Reference

Prefer expression bodies for constructors

Example

public Customer(int age) => Age = age;

csharp_style_expression_bodied_methods

Reference

Prefer expression bodies for methods

Example

public int GetAge() => this.Age;

csharp_style_expression_bodied_operators

Reference

Prefer expression bodies for operators

Example

public static ComplexNumber operator + (ComplexNumber c1, ComplexNumber c2)
    => new ComplexNumber(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);

csharp_style_expression_bodied_properties

Reference

Prefer expression bodies for properties

Example

public int Age => _age;

csharp_style_expression_bodied_indexers

Reference

Prefer expression bodies for indexers

Example

public T this[int i] => _values[i];

csharp_style_expression_bodied_accessors

Reference

Prefer expression bodies for accessors

Example

public int Age { get => _age; set => _age = value; }

csharp_style_expression_bodied_lambdas

Reference

Prefer expression bodies for lambdas

Example

Func<int, int> square = x => x * x;

csharp_style_expression_bodied_local_functions

Reference

Prefer expression bodies for local functions

Example

void M()
{
    Hello();
    void Hello() => Console.WriteLine("Hello");
}

Pattern matching Preferences

csharp_style_pattern_matching_over_as_with_null_check

Reference

Prefer pattern matching instead of as expressions with null checks to determine if something is of a particular type

Example

if (o is string s) {...}

csharp_style_pattern_matching_over_is_with_cast_check

Reference

Prefer pattern matching instead of is expressions with type casts

Example

if (o is int i) {...}

csharp_style_prefer_switch_expression

Reference

Prefer to use a switch expression (introduced with C# 8.0)

Example

return x switch
{
    1 => 1 * 1,
    2 => 2 * 2,
    _ => 0,
};

csharp_style_prefer_not_pattern

Reference

Prefer to use ‘not’ pattern, when possible (introduced with C# 9.0)

Default is true

Example

var y = o is not C c;

Code block Prerferences

csharp_prefer_braces

Reference

Prefer curly braces even for one line of code

Example

if (test) { this.Display(); }

csharp_prefer_simple_using_statement

Reference

Don't prefer to use a simple using statement

Example

using (var a = b) { }

File Header Preferences

file_header_template

Reference

unset or empty string - Do not require file header.

Default is unset

Example

namespace N2
{
    class C2 { }
}

Naming Rules

Non-private static fields are PascalCase

Example

public static MyString = "value";
protected static MyString = "value";
internal static MyString = "value";
protected_internal static MyString = "value";
private_protected static MyString = "value";;

Constants are PascalCase

Example

public const string MyConstant = "value";

Static fields are camelCase and start with s_

Example

private static int s_myInt;

Instance fields are camelCase and start with _

Example

private int _myInt;

internal string _myString;

Locals and parameters are camelCase

Example

private static string GetText(string path, string filename)
{
     var reader = File.OpenText($"{AppendPathSeparator(path)}{filename}");
     var text = reader.ReadToEnd();
     return text;

     string AppendPathSeparator(string filepath)
     {
        return filepath.EndsWith(@"\") ? filepath : filepath + @"\";
     }
}

Local functions are PascalCase

Example

private static string GetText(string path, string filename)
{
     var reader = File.OpenText($"{AppendPathSeparator(path)}{filename}");
     var text = reader.ReadToEnd();
     return text;

     string AppendPathSeparator(string filepath)
     {
        return filepath.EndsWith(@"\") ? filepath : filepath + @"\";
     }
}

By default, name items with PascalCase

Example

public void MyMethod() { };

Formatting Rules

dotnet_sort_system_directives_first

Reference

Sort System.* using directives alphabetically, and place them before other using directives.

Example

using System.Collections.Generic;
using System.Threading.Tasks;
using Avro;

dotnet_separate_import_directive_groups

Reference

:exclamation: Not defined :exclamation:

Example

// dotnet_separate_import_directive_groups = true
using System.Collections.Generic;
using System.Threading.Tasks;

using Avro;

// dotnet_separate_import_directive_groups = false
using System.Collections.Generic;
using System.Threading.Tasks;
using Avro;

dotnet_style_namespace_match_folder

Reference

:exclamation: Not defined :exclamation:

Example

// dotnet_style_namespace_match_folder = true
// file path: Example/Convention/C.cs
using System;

namespace Example.Convention
{
    class C
    {
    }
}

// dotnet_style_namespace_match_folder = false
// file path: Example/Convention/C.cs
using System;

namespace Example
{
    class C
    {
    }
}

Unnecessary Code Rules

Simplify name (IDE0001)

Reference Example

using System.IO;
class C
{
    // IDE0001: 'System.IO.FileInfo' can be simplified to 'FileInfo'
    System.IO.FileInfo file;

    // Fixed code
    FileInfo file;
}

Simplify member access (IDE0002)

Reference Example

static void M1() { }
static void M2()
{
    // IDE0002: 'C.M1' can be simplified to 'M1'
    C.M1();

    // Fixed code
    M1();
}

Remove unnecessary cast (IDE0004)

Reference Example

// Code with violations
int v = (int)0;

// Fixed code
int v = 0;

Remove unnecessary import (IDE0005)

Reference Example

// Code with violations
using System;
using System.IO;    // IDE0005: Using directive is unnecessary
class C
{
    public static void M()
    {
        Console.WriteLine("Hello");
    }
}

// Fixed code
using System;
class C
{
    public static void M()
    {
        Console.WriteLine("Hello");
    }
}

Remove unreachable code (IDE0035)

Reference Example

// Code with violations
void M()
{
    throw new System.Exception();

    // IDE0035: Remove unreachable code
    int v = 0;
}

// Fixed code
void M()
{
    throw new System.Exception();
}

Remove unused private member (IDE0051)

Reference Example

// Code with violations
class C
{
    // IDE0051: Remove unused private members
    private readonly int _fieldPrivate;
    private int PropertyPrivate => 1;
    private int GetNumPrivate() => 1;

    // No IDE0051
    internal readonly int FieldInternal;
    private readonly int _fieldPrivateUsed;
    public int PropertyPublic => _fieldPrivateUsed;
    private int GetNumPrivateUsed() => 1;
    internal int GetNumInternal() => GetNumPrivateUsed();
    public int GetNumPublic() => GetNumPrivateUsed();
}

// Fixed code
class C
{
    // No IDE0051
    internal readonly int FieldInternal;
    private readonly int _fieldPrivateUsed;
    public int PropertyPublic => _fieldPrivateUsed;
    private int GetNumPrivateUsed() => 1;
    internal int GetNumInternal() => GetNumPrivateUsed();
    public int GetNumPublic() => GetNumPrivateUsed();
}

Remove unread private member (IDE0052)

Reference Example

class C
{
    // IDE0052: Remove unread private members
    private readonly int _field1;
    private int _field2;
    private int Property { get; set; }

    public C()
    {
        _field1 = 0;
    }

    public void SetMethod()
    {
        _field2 = 0;
        Property = 0;
    }
}

// Fixed code
class C
{
    public C()
    {
    }

    public void SetMethod()
    {
    }
}

csharp_style_unused_value_expression_statement_preference

Reference

Prefer to assign an unused expression to a discard

Default is discard_variable

Example

_ = System.Convert.ToInt32("35");

csharp_style_unused_value_assignment_preference

Reference

Prefer to use a discard when assigning a value that's not used

Default is discard_variable

Example

int GetCount(Dictionary<string, int> wordCount, string searchWord)
{
    _ = wordCount.TryGetValue(searchWord, out var count);
    return count;
}

dotnet_code_quality_unused_parameters

Reference

Flag methods with any accessibility that contain unused parameters

Default is all

Example

public int GetNum1(int unusedParam) { return 1; }
internal int GetNum2(int unusedParam) { return 1; }
private int GetNum3(int unusedParam) { return 1; }

dotnet_remove_unnecessary_suppression_exclusions

Reference

enables the rule for all rule IDs and rule categories

Default is none

Example

using System.Diagnostics.CodeAnalysis;

class C1
{
    // 'dotnet_remove_unnecessary_suppression_exclusions = IDE0051'

    // Unnecessary pragma suppression, but not flagged by IDE0079
#pragma warning disable IDE0051 // IDE0051: Remove unused member
    private int UsedMethod() => 0;
#pragma warning restore IDE0051

    public int PublicMethod() => UsedMethod();
}

Remove unnecessary suppression operator (IDE0080)

Reference Example

// Code with violations
if (o !is string) { }

// Potential fixes:
// 1.
if (o is not string) { }

// 2.
if (!(o is string)) { }

// 3.
if (o is string) { }

Remove unnecessary equality operator (IDE0100)

Reference Example

// Code with violations
if (x == true) { }
if (M() != false) { }

// Fixed code
if (x) { }
if (M()) { }

Remove unnecessary discard (IDE0110)

Reference Example

// Code with violations
switch (o)
{
    case int _:
        Console.WriteLine("Value was an int");
        break;
    case string _:
        Console.WriteLine("Value was a string");
        break;
}

// Fixed code
switch (o)
{
    case int:
        Console.WriteLine("Value was an int");
        break;
    case string:
        Console.WriteLine("Value was a string");
        break;
}

Miscellaneous Rules

Remove invalid global ‘SuppressMessageAttribute’ (IDE0076)

Reference Example

// IDE0076: Invalid target '~F:N.C.F2' - no matching field named 'F2'
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Category", "Id: Title", Scope = "member", Target = "~F:N.C.F2")]
// IDE0076: Invalid scope 'property'
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Category", "Id: Title", Scope = "property", Target = "~P:N.C.P")]

// Fixed code
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Category", "Id: Title", Scope = "member", Target = "~F:N.C.F")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Category", "Id: Title", Scope = "member", Target = "~P:N.C.P")]

namespace N
{
    class C
    {
        public int F;
        public int P { get; }
    }
}

Avoid legacy format target in global ‘SuppressMessageAttribute’ (IDE0077)

Reference Example

// IDE0077: Legacy format target 'N.C.#F'
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Category", "Id: Title", Scope = "member", Target = "N.C.#F")]

// Fixed code
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Category", "Id: Title", Scope = "member", Target = "~F:N.C.F")]

namespace N
{
    class C
    {
        public int F;
    }
}