views:

313

answers:

8

In C#(.NET), what are the regular "formatting" standards?

For instance, for parameter names, so far I noticed camelCase with no prefix, is that right? For object names, camelCase and no prefix either, right?

For namespaces, classes, functions and properties, first letter of the word is capitalized and there is no prefix, is this right (again)?

How are "temporary" objects formatted?

Example:

namespace TestNamespace
{
    class MyOwnCoolClass
    {
        MyOwnCoolClass(int length, BinaryWriter writer)
        {
            BinaryWriter tempbw = new BinaryWriter(length);
            return tempbw;
        }
    }
}


(Note: This code is not valid, I know, it's just to demonstrate formatting). Thanks!

+5  A: 

Full guidelines from Microsoft can be found in the Class Design Guidelines. It's old, but I haven't yet found anything updated on MSDN, and I believe it is still the standard set reccomendations

David Stratton
See my post for a link to the updated guidelines. :)
280Z28
Here is the updated link: http://msdn.microsoft.com/en-us/library/ms229042.aspx
Scott Dorman
Thanks for you answer, but 280Z28's answer fitted my question more. :) Still, +1
Lazlo
+13  A: 

If you use StyleCop, you'll get good consistency with the Microsoft .NET Design Guidelines for Developing Class Libraries. StyleCop also enforces several additional things like spacing, ordering of items in a file, and naming of non-public code elements.

280Z28
+1 took the words right out of my fingers!
curtisk
I don't plan on using StyleCop, but the link fills exactly what I needed. Thank you!
Lazlo
A: 

I would take a look at the following Coding Practices.

It covers virtually everything that I see in most C# code that I read...including prefixing UI elements. I personally don't follow it to the letter but it gives you a good idea of the general standard and what is good/bad practice.

Hawker
A: 

There are references to Microsoft coding standards. Visual Studio 2005 and 2008 have a Code Analysis engine built in that will guide you through these coding standards and also point you towards some best practices too. Right click on your project and click "Run Code Analysis on project".

Whenever an invalid or less preferred code portion is spotted, it will not only warn you, but point you to a Help Resource on the FxCop site.

Dominic Zukiewicz
A: 

MSDN: Naming Guidelines

I tend to follow how the .NET framework is coded so my code is consistent with .NET Framework.

My only personal preference is the naming of class variables which I use the prefix "m_" before each variable like private int m_count.

Francis B.
A: 

First, I would say there aren’t any regular formatting standards but Microsoft provide one:

Most people/projects use standards that are close to Microsoft's guidelines but vary depending on the team members personal preferences. Most of the variation is with private/protected members and not public ones.

Here is another commonly used coding standards document:

Scott Dowding
A: 

You can use the combination of StyleCop and FxCop (or the built-in Code Analysis for VS2008 Pro) for tools and the Framework Design Guidelines.

Scott Dorman
+2  A: 
David Anderson
As a solo dev who has never had anyone give him any guidelines or conventions I find this very informative. I am curious though how you would deal with `var` and the code pages of a Form. I see the section on "Source File Layout" and I wonder if that holds true for WinForm code pages as well. Also, under "Controls and Components", you state that `ux` should be used to prevent coding changes later. Is that, then, the same mentality for using var? Thanks for the doc!
Refracted Paladin
Yeah, the same goes for forms. The new version will contain more goodies like that. I would normally region control events separate from methods. Normally my forms never have methods though, because I always create a service layer separate from the UI layer.I'm inclined to finish the latest version a bit quicker, so I'll post back here when it's published. Keep an eye out. ;)Thanks for the great comment by the way!
David Anderson
@Refracted Paladin: We use the "ux" prefix to prevent changing a UI Control later on in development. e.g. Hungarian Conventions use means if you change a textbox to a combo box, you would change the name from "txtMyTextBox" -> "cmbMyComboBox" if you're a stickler for naming conventions. Also, "m_" and "ux" prefixes group variables and controls in Intellisense.
Zack
Thanks, to both of you. @DavidA: Any idea of a Timeframe so I know when to start checking back? Month, Week, Day, Year???
Refracted Paladin
Two days, tops. ;)
David Anderson
New version is done and uploaded. You can download it using the same link. I usually update this document once every couple of months with major updates
David Anderson
You're a Saint! I Sounds silly but you have no idea how much this helps someone in my position....Thank you.
Refracted Paladin
I must have jumped the gun...as the one there is still the same as yesterdays....I'll check again later.
Refracted Paladin
Should be the new version for February 2009. Just uploaded and verified the link a couple of minutes ago. If it's not working for you (browser cache issue?) I'll be more than happy to email it to you.
David Anderson
There's also a docx version as well:http://www.dcomproductions.com/downloads/devguide/dcguide.docx
David Anderson
Okay, got it. Chrome was refusing to release my cache, strange.....I guess I get to visit ***SuperUser*** now! Anyway I got my hands on it now and I am grateful. If you are ever in LaCrosse WI let me know and I'll buy you a beer.*(once you are 21!!! 19! now I feel OLD and DUMB!)*
Refracted Paladin
You're very welcome! I hope my conventions are a benefit to you, as everyone else who downloads them ;)
David Anderson