Standardized formatting configuration for C# projects using K&R/Stroustrup style
This .editorconfig file provides consistent code formatting configuration for C# projects, following the K&R/Stroustrup style. It ensures that all project developers maintain the same formatting standards, regardless of the editor or IDE used.
- Indentation: 4 spaces
- Encoding: UTF-8
- Line ending: CRLF (Windows)
- Trailing whitespace removal
- Insert final newline
// β
Applied style
if (condition) {
// code
} else {
// code
}
try {
// code
} catch (Exception ex) {
// handling
} finally {
// cleanup
}- Binary operators: Spaces before and after (
a + b) - Assignment operators: Spaces before and after (
x = y) - Conditional operators: Spaces before and after (
a ? b : c) - Cast: No space after cast (
(int)value) - Control keywords: Space after (
if (,for (,while () - Parentheses: No internal spaces
- Case content: Indented
- Switch labels: Indented
- Labels: Left-aligned
-
Copy the
.editorconfigfile to the root of your C# project -
Make sure your editor supports EditorConfig:
- β Visual Studio
- β Visual Studio Code
- β JetBrains Rider
- β Vim/Neovim
- β Emacs
- β Sublime Text
-
Restart your editor if necessary
project/
βββ .editorconfig # β This file
βββ src/
β βββ *.cs
βββ tests/
βββ *.cs
using System;
using System.Collections.Generic;
namespace MyProject {
public class MyClass {
private readonly List<string> _items;
public MyClass() {
_items = new List<string>();
}
public void AddItem(string item) {
if (string.IsNullOrEmpty(item)) {
throw new ArgumentException("Item cannot be empty");
}
_items.Add(item);
}
public string ProcessItem(string item) {
return item switch {
null => "Null",
"" => "Empty",
_ => item.ToUpper()
};
}
}
}| Editor/IDE | Native Support | Plugin Required |
|---|---|---|
| Visual Studio | Yes | No |
| VS Code | Yes | No |
| JetBrains Rider | Yes | No |
| Vim/Neovim | No | Yes |
| Emacs | No | Yes |
| Sublime Text | No | Yes |
Feel free to suggest improvements or adjustments to this configuration through issues or pull requests!
Written by Murillo Yonamine (Momodev)