vimwiki

C# Conventions

Naming conventions:

public class MyClass
{
    public void MyMethod()
    {
        // Code here
    }
}
public void DoSomething(int myParameter)
{
    string myVariable = "Hello";
    // Code here
}

summary

type format exemple
function PacalCase MyFunction
function parameter camelCase myParameter
constants UPPERCASE_SnakeCase CONSTANT_NAME
properties PacalCase PropertiesName
event PacalCase EventName
fields camelCase EventName

Indentation:

public void MyMethod()
{
    if (someCondition)
    {
        // Code inside braces is indented four spaces
        // ...
    }
    else
    {
        // ...
    }
}

Comments:

// Single-line comments for short comments
public void MyMethod()
{
    // This is a short comment
    // ...
}

/// XML documentation comments for documenting classes, methods, and parameters
/// <summary>
/// This is a summary of the class or method
/// </summary>
public class MyClass
{
    /// <summary>
    /// This is a summary of the method
    /// </summary>
    /// <param name="myParameter">This is a summary of the parameter</param>
    public void MyMethod(int myParameter)
    {
        // ...
    }
}

Braces:

public void MyMethod()
{
    if (someCondition)
    {
        // Opening brace on the same line as the statement or declaration
        // Closing brace on its own line
        // ...
    }
    else
    {
        // ...
    }
}

Line length:

public void MyMethod()
{
    // Keep lines shorter than 80 characters
    string myString = "This is a long string that should be broken up into multiple lines " +
                      "to improve readability.";
}

Formatting:

public void MyMethod()
{
    // Use whitespace to separate logical blocks of code
    if (someCondition)
    {
        // ...
    }

    // Use vertical whitespace to improve readability
    // ...
    // ...
}

Exception handling:

public void MyMethod()
{
    try
    {
        // Code that might throw an exception
    }
    catch (Exception ex)
    {
        // Handle the exception
        throw new Exception("An error occurred: " + ex.Message);
    }
}

Enums:

// Enum names in PascalCase
public enum Color
{
    Red,
    Green,
    Blue
}

Boolean values:

public void MyMethod()
{
    bool isReady = true;
    bool hasValue = false;

    // Use descriptive boolean variable names
    if (isReady)
    {
        // ...
    }

    if (!hasValue)
    {
        // ...
    }
}

Constants:

// Constants in all uppercase
public const int MAX_VALUE = 100;

// Use descriptive names for constants
public const string ERROR_MESSAGE = "An error occurred.";