C#

Always capitalize and space comments using //

// Good
if (comments)
{
  // Something
}

Always newline curly bracket

// Good
if (newLineCurly)
{
  // Something
}

Always one newline after blocks of code

// Good
void myMethod()
{
  if (firstBlock)
  {
    // Block
  }

  if (anotherBlock)
  {
    // Block
  }

}

Always camel case or pascal case

// Good
class MyPascalCaseClass
void MyPascalCaseMethod()
public struct MyPascalCaseStruct
int myCamelCaseVariable = 0;

Favor verbatim string literal

// Good
string myStringWithBackslash @"\my_file";
// Bad
string myStringWithBackslash "\\my_file";

Always space between parenthesize and if

// Good
if (comments)

Always curly brace block for if

// Good
if (comments)
{
  // Some code
}

// Bad
if (comments)
  // Some code

Only use var when it is obvious

Private and public naming

Internalize these public and private organization strategy examples.

// Good
public class MyUpperCasedPublic
{
  private int myPrivateMember;
}
// Good variable access
class MyClass
{
  // Use public get and set accessor over public member variable
  public int MyAccessor {get; set;}

  private int myInternalMember;

  public int MyMethod(int myLocalParam)
  {
    // Private accessor (internal state) are accessed with the this keyword
    int myLocalVar = this.myInternalMember;
    // Public accessor are accessed without this keyword
    MyAccessor = myLocalParam;
    // Local vars and local params reference by name
    myLocalParam = myLocalVar;
    return 0;
  }

}
// Good method naming
class MyClass
{
  // Pascal case public interfaces
  public int MyMethod()
  {
    return 0;
  }

  // camel case private methods
  private int myInternalMethod()
  {
    return 0;
  }

}
// Good this usage
class MyClass
{
  public int MyPublicMethod()
  {
    return this.myPrivateMethod();
  }

  private int myPrivateMethod()
  {
    return MyPublicMethod();
  }

}

Class organization

Internalize this class organization example.

// Good
class MyClass
{
  // private variables
  // public variables

  // private accessors
  // public accessors

  // private delegates
  // public delegates

  // private constructors
  // public constructors

  // private methods
  // public methods

  #region Sub region description
  // private variables
  // public variables

  // private accessors
  // public accessors

  // private delegates
  // public delegates

  // private constructors
  // public constructors

  // private methods
  // public methods
  #endregion
}

Test naming convention

Good file name PollEverywhere.MyClassUnderTest.TestMyFeatureEnableDisable.cs

Template

namespace PollEverywhere.Test
{
  /// <summary>
  /// </summary>
  [TestClass]
  public class TestMyFeatureEnableDisable
  {
    // private TestContext testContextInstance;

    public TestMyFeatureEnableDisable()
    { }

    // public TestContext TestContext
    // {
    //   get
    //   {
    //     return testContextInstance;
    //   }
    //   set
    //   {
    //     testContextInstance = value;
    //   }
    // }

    #region Additional test attributes
    //
    // You can use the following additional attributes as you write your tests:
    //
    // Use ClassInitialize to run code before running the first test in the class
    // [ClassInitialize()]
    // public static void MyClassInitialize(TestContext testContext)
    // { }
    //
    // Use ClassCleanup to run code after all tests in a class have run
    // [ClassCleanup()]
    // public static void MyClassCleanup()
    // { }
    //
    // Use TestInitialize to run code before running each test
    // [TestInitialize()]
    // public void MyTestInitialize()
    // { }
    //
    // Use TestCleanup to run code after each test has run
    // [TestCleanup()]
    // public void MyTestCleanUp()
    // { }
    #endregion

    [TestMethod]
    [TestCategory("MyCategory")]
    public void MyFirstTest()
    { }

  }

}

Can’t find a standard?

When in doubt follow the maintainer’s standard