views:

217

answers:

8

What are the most common naming conventions in C# for classes, namespaces and methods? Is it common to have getter/setter style methods as in Java?

+5  A: 

No it is not common to use getter /setter style names in C#. Properties should be used for almost all places you would use a getter / setter in Java.

IMHO, the defacto standard for naming conventions comes from the Framework Design Guidelines. It's enforced by several tools (FxCop) and is the dominant style of many libraries including the BCL.

JaredPar
Take note that C# 3.0 + provides support for auto-implementing properties. And don't ignore private set.
Brian
While correct this doesn't really give the whole picture. I know that you know this, but I'd like to see the answer reference that properties, in essence, are syntactic sugar for accessors and are preferred over explicit accessors. When using a Property you really **are** defining accessor methods. As of C# 3.0, the language provides automatic properties that will create backing fields and the appropriate methods to get/set them via the automatic property syntax. Coming from a Java background it took me awhile to catch on to properties until I understood what they really were.
tvanfosson
A: 

A quick google search gives an msdn article.

Strilanc
+8  A: 

Guidelines for Names (from Design Guidelines for Developing Class Libraries, in which you will also find sections about properties and choosing between properties and methods. In that last article, you will find the following:

Do use a property, rather than a method, if the value of the property is stored in the process memory and the property would just provide access to the value.

Fredrik Mörk
Thanks for the edit @ShuggyCoUk; missed that.
Fredrik Mörk
+2  A: 

I think that most projects are using the IDesign CSharp Coding Standard

Shay Erlichmen
A: 

MSDN provides Design Guidelines that spell this out in detail.

Reed Copsey
+1  A: 

There are a few common patterns out there. One I frequently see and use in my own projects is:

namespace Company.Concept.SubConcept
{
    public class MyClass
    {
        private MyType someData;

        public MyType SomeData
        { 
            get { return someData; }
            set { someData = value; }
        }

        public void MyMethod()
        {
        }
    }
}
Eric J.
Personally I'm not a fan of the m_ conventions. The goal of that notation is to tell you the scope of the variable. However, I find that it gets in the way of readability of the code more than it helps understand where the variable is declared. If you are in doubt of where the variable is defined, just mouse over it in Visual Studio. I feel this standard is going the same route that Hungarian notation went (remember all the variable prefixes to remind you of the variable's type? n, l, sz, dw, lpsz, etc?)
Eric J.