views:

194

answers:

2

After you've been programming for a long time with a language, you pick up certain coding standards or styles. With Delphi it's things like prefixing private variables with f and putting private declarations before protected, which in turn are before public ones etc etc. Most of this comes from the VCL.

Is there any recognized coding standard or style in the C# world? I'm tempted to put an f in front of my private member variables but this would only make sense to other Delphi developers.

+6  A: 

Check out the following. It's Microsoft's take on the question.

http://msdn.microsoft.com/en-us/library/czefa0ke(VS.71).aspx

Just talking about the very basics for c#: Private class fields are prefaced with an underscore. Public class members are capitalized. Method parameters are lower case. For example:

public class Person {
  private String _personName;
  public String PersonName { 
    get { return _personName; }
    set { _personName = value; }
  }

  public String SayHello(String toName ) {
    return String.Format("Hi {0}, I am {1}", toName, PersonName);
  }

}

Note that in order to be CLS compliant, you should not depend on casing to differentiate between variables. Also, it's considered bad practice to preface parameters or other variables with the type OR with some form of hungarian notation (which nobody gets right anyway. Read this).

The only reason private fields are prefaced with an underscore is to differentiate them from method parameters.

One practice I run into a lot is the use of the "this." notation. Besides being annoying, it's completely unnecessary when things are named correctly.

Chris Lively
Just what I was looking for. I guess I could have googled it! Thanks any way.
Steve
A: 

Standards are many and varied but by far the most common convention is Pascal casing and either underscore or no prefix at all for private member fields.

I refer you to MS's coding standards here (deep subject!)

Also consider using StyleCop to train yourself and enforce standards, but be advised it's not perfect and it's not that... conventional in a lot of respects. I'd recommend for an initial use period you review the rules as they come up and turn off what you dislike.

annakata