Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

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
}

or:

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

should I do this:

Car car = new Car();

or:

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.

share|improve this question

marked as duplicate by Michael Kohne, Dan Pichelman, DougM, amon, Dynamic Mar 4 at 22:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
please don't break your declarations across lines. you'll give me histrionics again. –  Jimmy Hoffa Feb 6 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 –  aDev Feb 6 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 at 23:39

3 Answers 3

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.

http://stylecop.codeplex.com/

http://msdn.microsoft.com/en-us/library/bb429476(v=vs.80).aspx

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

share|improve this answer
    
I also use the first two examples way ... :) –  aDev Feb 6 at 22:05
1  
Also, make heavy use of Ctrl+K,Ctrl+D and/or Ctrl+K,Ctrl+F. –  Brian Feb 6 at 22:32
    
Or Ctrl+Alt+F if you're using ReSharper, and really who isn't :P –  Klee Feb 6 at 22:37
    
For FxCop 10 download, see stackoverflow.com/a/12429732/283561 –  Simon Feb 7 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 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.

share|improve this answer

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.

share|improve this answer

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