Skip to content

Instantly share code, notes, and snippets.

@KaiserWerk
Last active July 6, 2025 09:41
Show Gist options
  • Save KaiserWerk/5f73bf419436ab791975b7b5f3bff776 to your computer and use it in GitHub Desktop.
Save KaiserWerk/5f73bf419436ab791975b7b5f3bff776 to your computer and use it in GitHub Desktop.
Coding guidelines summary for C#

Coding Guidelines (C#)

A summary of the official Microsoft C# coding conventions, based on Microsoft’s documentation.


1. Naming Conventions

  • PascalCase for class, property, method, and namespace names
    public class MyExampleClass { }
  • camelCase for local variables and method parameters
    int itemCount;
  • Private fields: camelCase (underscore prefix is not allowed)
    private int itemCount;
  • Interface names: Prefix with I
    public interface IMyInterface { }
  • No Hungarian notation or type in variable names.
  • Use descriptive, meaningful names.

2. Layout and Formatting

  • Indentation: 4 spaces per indentation level (no tabs).
  • Curly braces:
    • For types and methods: Opening brace on a new line
      public class MyClass
      {
      }
    • For other blocks (if, else, etc.): Opening brace on a new line
      if (true) 
      {
      }
  • Blank lines: Separate logical sections with blank lines for readability.
  • Line length: Prefer lines under 120 characters.

3. Organizing Code

  • Place using directives at the top of the file, outside the namespace.
  • Group similar code elements (fields, constructors, methods).
  • Order elements by accessibility: public, internal, protected, private.

4. Language Features

  • Prefer var when the type is obvious or unimportant.
  • Otherwise, use explicit type names for clarity.
  • Use expression-bodied members for simple property or method definitions.
  • Prefer modern C# features (pattern matching, interpolated strings, etc.) where appropriate.

5. Comments

  • Use XML documentation comments (///) for public APIs.
  • Write clear, concise comments only where necessary.
  • Avoid stating the obvious.

6. Strings and Literals

  • Use verbatim strings (@"") for file paths and regular expressions.
  • Prefer string interpolation ($"Value is {value}") over concatenation.

7. Error Handling

  • Use exceptions for error handling, not return codes.
  • Catch only exceptions you can handle.
  • Avoid empty catch blocks (never swallow exceptions silently).

8. Other Notable Conventions

  • Prefer auto-properties for simple property definitions.
  • Use collection and object initializers for clarity.
  • Avoid regions except for generated or extremely large code files.

This summary is based on the official Microsoft documentation and is intended as a quick reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment