public
Last active

Examples of C# code conventions for http://sideeffect.kr/popularconvention/

  • Download Gist
csharp-conventions.md
Markdown

Space vs Tab

Space

public string GetSomething()
{
    return something;
}

Tab

public string GetSomething()
{
    return something;
}

Block Statements

Curlybrace with one space

if (height < MinHeight) {
  //...
}

while (isTrue) {
  //...
}

switch (foo) {
  //...
}

Curlybrace withat new line

if (height < MinHeight)
{
  //...
}

while (isTrue)
{
  //...
}

switch (foo)
{
  //...
}

Curlybrace with no space

if (height < MinHeight){
  //...
}

while (isTrue){
  //...
}

switch (foo){
  //...
}

Constant name

Constant is Pascal cased

const string FooBar = "baz";

Constant is all caps with underscore

const string FOO_BAR = "baz";

Conditionals

Condition with space

if (true) {
  //...
}

while (true) {
  //...
}

switch (v) {
  //...
}

Condition with no space

if(true) {
  //...
}

while(true) {
  //...
}

switch(v) {
  //...
}

Method arguments with one space vs no space

No space

public void SetName(string name) {
  // ...
}

if(isTrue) {}

while(isTrue) {}

One space

public void SetName( string name ) {
  // ...
}

if( isTrue ) {}

while( isTrue ) {}

Line Length over 80 characters

Line length is within 80 characters.

/* width is within 80 characters */

Line length is within 120 characters.

/* width is within 120 characters */

Line length is within 150 characters.

/* width is within 150 characters */

Special prefix for static var

No special prefix

static string name;

Special prefix

static string s_name;

Double underscore prefix

static string __name;

Private member naming

no underscore

string property;

public string Property { get { return property; } }

underscore

string _property;

public string Property { get { return _property; } }

m prefix

string m_property;

public string Property { get { return m_property; } }

Private method casing

Pascal casing as God intended

private void DoSomething()
{}

camel casing for people who hate the world

private void doSomething()
{}

What about elses?

}
else 
{

} else {

}
else {

These are the 3 main styles I think

resharper has great examples of syntax for it's formatting settings. There are way more in there than probably necessary but a good place to get ideas.

Not to be knit-picky, but you've got a typo:

"Curlybrace withat new line"

What about braces and spacing for single-statement blocks?

Single statement blocks formatted like multi-statement blocks

if(foo)
{
    Console.WriteLine("bar");
}

versus no braces, but with a newline

if(foo)
    Console.WriteLine("bar");

versus no braces, no newline

if(foo) Console.WriteLine("bar");

The same thing can be done for property getters & settings.

A few more:

  1. Specifying the default accessor for classes and members (or not)
  2. Protected internal vs internal protected accessor
  3. for vs foreach (might be a bit tricky given the regex parsing of this project)
  4. LINQ comprehension syntax (select, from, where) vs Lambda syntax (Select(c => ...).Where(c => ...))
  5. Attributes all on one line or one per line
  6. Putting : base(...) on same or subsequent line

You could go as far as include all the rules under the Layout, Naming, Spacing, Readability categories of StyleCop.
http://www.stylecop.com/docs/StyleCop%20Rules.html

How about implicit vs explicit variable declaration:

int myValue = Compute();
// vs
var myValue = Compute();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.