Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Examples of C# code conventions for http://sideeffect.kr/popularconvention/

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()
{}
@distantcam

This comment has been minimized.

Copy link

commented Sep 17, 2013

What about elses?

}
else 
{

} else {

}
else {

These are the 3 main styles I think

@steveoh

This comment has been minimized.

Copy link

commented Sep 18, 2013

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.

@jacobappleton

This comment has been minimized.

Copy link

commented Sep 18, 2013

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

"Curlybrace withat new line"

@breischl

This comment has been minimized.

Copy link

commented Sep 18, 2013

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.

@damieng

This comment has been minimized.

Copy link

commented Sep 20, 2013

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
@DavidDeSloovere

This comment has been minimized.

Copy link

commented Sep 25, 2013

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

@iskeld

This comment has been minimized.

Copy link

commented Nov 27, 2013

How about implicit vs explicit variable declaration:

int myValue = Compute();
// vs
var myValue = Compute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.