Skip to main content
Bitwarden Server follows consistent code style guidelines enforced through EditorConfig and automated formatting tools.

EditorConfig

The project uses .editorconfig to maintain consistent coding styles:
.editorconfig

Automatic Formatting

dotnet format

The project uses dotnet format for automatic code formatting. Run formatting manually:
Enable pre-commit hook:
This automatically formats code before each commit.

IDE Integration

Visual Studio Code:
  • Install C# Dev Kit extension
  • Format on save is configured in .vscode/settings.json
Visual Studio / Rider:
  • EditorConfig is automatically detected
  • Use built-in formatter (Ctrl+K, Ctrl+D)

C# Coding Conventions

Naming Conventions

PascalCase

Use PascalCase for:
  • Classes
  • Interfaces (with I prefix)
  • Methods
  • Properties
  • Events
  • Enums and enum values
  • Constants

camelCase

Use camelCase for:
  • Private fields (with _ prefix)
  • Method parameters
  • Local variables

Interface Naming

Interfaces start with I:

Async Method Naming

Async methods end with Async:

Code Organization

File-Scoped Namespaces

Use file-scoped namespaces (C# 10+):

Using Directives

Sort using directives with System.* first:
Remove unused using directives (enforced by IDE0005 warning).

Member Ordering

Order class members:
  1. Fields (private, then protected)
  2. Constructors
  3. Properties (public, then protected/private)
  4. Methods (public, then protected/private)
  5. Nested types

Language Features

var Keyword

Prefer var when the type is obvious:

String Interpolation

Use string interpolation over concatenation:
Important: Specify culture for formatting:

Null Handling

Null-conditional operators:
Null-coalescing operators:
Nullable reference types:

Pattern Matching

Use pattern matching where appropriate:

Braces and Formatting

Brace Style

Always use braces, even for single-line statements:
Opening braces on new line (Allman style):

Line Length

Keep lines under 120 characters (guideline, not strict rule). Break long lines at logical points:

Comments and Documentation

XML Documentation

Add XML comments for public APIs:

Inline Comments

Use inline comments to explain “why”, not “what”:

TODO Comments

Include issue numbers when applicable:

Error Handling

Exceptions

Use specific exception types:

Try-Catch

Catch specific exceptions when possible:

Async/Await

Async All the Way

ConfigureAwait

Library code should use ConfigureAwait(false) (though not strictly enforced):

LINQ and Collections

LINQ Style

Collection Initialization

Testing Code Style

Test Naming

Format: MethodName_Scenario_ExpectedOutcome

Arrange-Act-Assert

Always structure tests with clear AAA sections:

Common Analyzer Rules

The project enforces several code analysis rules:

CA1304 & CA1305: Specify CultureInfo

IDE0005: Remove Unnecessary Usings

CS8509: Missing Switch Case

See Also