Skip to content

MurilloYonamine/Csharp-Stroustrup-Style

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 

Repository files navigation

🎨 EditorConfig for C#

Standardized formatting configuration for C# projects using K&R/Stroustrup style

EditorConfig C#

πŸ“‹ About

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.

✨ Features

πŸ”§ Basic Settings

  • Indentation: 4 spaces
  • Encoding: UTF-8
  • Line ending: CRLF (Windows)
  • Trailing whitespace removal
  • Insert final newline

🎯 Brace Style (K&R/Stroustrup)

// βœ… Applied style
if (condition) {
    // code
} else {
    // code
}

try {
    // code
} catch (Exception ex) {
    // handling
} finally {
    // cleanup
}

πŸ“ Spacing Rules

  • 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

πŸ—οΈ Indentation

  • Case content: Indented
  • Switch labels: Indented
  • Labels: Left-aligned

πŸš€ How to Use

  1. Copy the .editorconfig file to the root of your C# project

  2. Make sure your editor supports EditorConfig:

    • βœ… Visual Studio
    • βœ… Visual Studio Code
    • βœ… JetBrains Rider
    • βœ… Vim/Neovim
    • βœ… Emacs
    • βœ… Sublime Text
  3. Restart your editor if necessary

πŸ“ Project Structure

project/
β”œβ”€β”€ .editorconfig      # ← This file
β”œβ”€β”€ src/
β”‚   └── *.cs
└── tests/
    └── *.cs

🎨 Formatting Example

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()
            };
        }
    }
}

βš™οΈ Compatibility

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

πŸ“š Additional Resources

🀝 Contributing

Feel free to suggest improvements or adjustments to this configuration through issues or pull requests!


Written by Murillo Yonamine (Momodev)

Releases

No releases published

Packages

No packages published