Code styling

C# styling

If there length of the parameter list for a method is too long to read conveniently in terms of line length (due to the 3-argument rule this should rarely happen for methods but constructors with dependency injection) break it into multiple lines parameter by parameter.

public class MyClass
{
    public MyClass(
        IDependency1 dependency1,
        IDependency2 dependency2,
        IDependency3 dependency3)
    {
        // ...
    }
}

Prefix private fields with an underscore (_).


Have a standard ordering of members depending on their visibility and whether they're instance- or class-level, etc.

//  Notice the order: static, private, protected, public, const, constructor, public, protected, private, static, inner classes
public class MyClass
{
    // Static fields first
    private static string _myStaticField = "field";

    // Private fields
    private string _myField = "field";

    protected string _myProtected = "field";

    // Properties next
    public int MyProperty { get; set; }

    // Constants just before the constructor
    public const string MyConst = "const";


    // Then the constructor(s)
    public MyClass()
    {
    }


    // Public methods
    public void MyMethod()
    {
    }


    // Protected methods
    protected void MyProtected()
    {
    }


    // Private methods
    private void MyPrivateMethod()
    {
    }


    // Static methods
    private static void MyStaticMethod()
    {
    }


    // Inner classes
    public class MyInnerClass
    {
    }
}

If an expression is short, omit line breaks when applicable to keep the code compact (as long as readability is not hurt), e.g.:

public class MyClass
{
    private int _myField;
    public int MyProperty { get { return _myField; } }
}

CSS styling

Structure your stylesheet's content logically under titles. Use the following comment formats for different levels of titles:

/* First-level title
***************************************************************/

// Second-level title
// -------------------------

/* Third-level title */

Use line breaks to space out blocks of code.

This page is part of the Orchard Dojo Library!

This page and many others here are part of the open-source, portable package of Orchard 1.x goodies: the Orchard Dojo Library for Orchard 1.x. The Library is freely accessible by anyone; just go to its repository to download its full content (you can even download the textual content of the Library as a big concatenated document in HTML), file issues or fork it! You can even edit it online on Bitbucket, just click on the Edit link in the upper right corner!

Note that the Library also has an Orchard Core version, check it out here!