I wanted to know if there are some default standards for writing c# code. By default I mean may be something officially from Microsoft, or any other organization.

I am looking for something where I can find answers to questions like following:

should I do this:

public void DoSomeThing(int id)
    //method body


public void DoSomeThing(int id) {
    //method body

should I do this:

Car car = new Car();


Car car = 
    new Car();

should I declare my variable in camel or pascal case, should I declare input parameters in method signature camel or pascal, should the global variable be prefixed with underscore or internal variable be prefixed with underscore ...

all these kind of questions ... any suggestions?

The actual issue is, I work on two projects for two different companies. and both have exactly opposite standards. I want to convince either of those and my self that such and such is the right way, because of such and such reason.

I know there is no one right way, but still any defaults documented from any leader organization would help. Thanks.

  • 2
    please don't break your declarations across lines. you'll give me histrionics again. Feb 6 '14 at 22:02
  • i agree ... i really want to do that way, but the 2nd project guys want to stick with that !!! ... i need a good reason to convince them to change that Feb 6 '14 at 22:04
  • Perhaps start at the source: msdn.microsoft.com/en-us/library/ff926074.aspx A definative document from Microsoft you can point others to.
    – Jon P
    Feb 6 '14 at 23:39

My suggestion would be to go with the Coding style product that are out there for Visual Studio. In particular Style Cop and FxCop. We set these up and use them on every build, we will even brake our CI builds for some of the cases. Thats a good automated way to enforce coding standards and you have an excuse for following one way because the code isn't going to compile if you don't.



For the record I would be using the first example in those two cases.

  • I also use the first two examples way ... :) Feb 6 '14 at 22:05
  • 1
    Also, make heavy use of Ctrl+K,Ctrl+D and/or Ctrl+K,Ctrl+F.
    – Brian
    Feb 6 '14 at 22:32
  • Or Ctrl+Alt+F if you're using ReSharper, and really who isn't :P
    – Klee
    Feb 6 '14 at 22:37
  • For FxCop 10 download, see stackoverflow.com/a/12429732/283561
    – Simon
    Feb 7 '14 at 7:34
  • 1
    I agree @Marc Climent you should make a decision as to what rules to use and to know why you've chosen those rules, StyleCop is a good tool to get that conversation started. For anyone that is wondering the difference between putting usings inside/outside the namespace is spelled out quite well in this SO answer stackoverflow.com/questions/125319/… I didn't know the difference until I started using StyleCop. Once you know it's up to you to decide where they go.
    – Klee
    Mar 1 '14 at 8:26

I would say StyleCop is the authoritative way of enforcing C# code style since it originally came from Microsoft. For any C# developer I can't recommend the combination of ReSharper and StyleCop enough -- it really makes a difference.

Code style can be something that starts all kinds of holy wars among programmers, and when I first started using StyleCop I ended up suppressing most of its suggestions (don't intermingle static and instance methods, pah!).

After a few years of using it and trying to abide by its standards I can say it really pains me to read code that's not StyleCop compliant.


The IDesign coding standard is widely respected. It sounds like your question is geared toward style, but in my opinion, a good coding standard should also include a "coding convention". That is, things you should or should not do to avoid language specific pitfalls.

Check out the IDesign standard; it has both of the above (style & convention). You can learn a ton just by reading through the thing.

Also, this question is a possible duplicate of this post.

Not the answer you're looking for? Browse other questions tagged or ask your own question.