C # formatting conventions

In the book I read, they capitalize on public methods and properties. I know there are some other conventions like you put "_" in front of private variables. For me I don't like this way and like this way is better, but just wondering about things in the method.

So,

public void MyMethod()
{
}

public string MyProperty {get; set;

}

      

and for private

private void myMethod()
{
}

      

But what about the method?

like

public void MyMethod()
{
   string MyVariable = null;
   // or
   string myVairable = null;
}

      

Also what about if you have a global variable like

public class Test
{
   private string bob;

   public Test()
   {
      bob = null;
   }
}

      

so it should be lowercase (since it is private)? Also on the side of the sticky note, it would be better to just make this a property, but instead of a public property, just have it private.

+2


source to share


7 replies


Here are your code examples as they would if they followed Microsoft's official code style guidelines (followed by StyleCop and FxCop)

public void MyMethod()
{
}

public string MyProperty { get; set; }

private void MyMethod()
{
}

public void MyMethod()
{
   string myVairable = null;
}

public class Test
{
   private string bob;

   public Test()
   {
      this.bob = null;
   }
}

      

Some highlights of the spec: All fields must be in private

lowercase (unless they are constant). All methods must be capitalized, regardless of access. If you want to open the field (i.e. Make it public

or protected

) use the property (which must be capitalized if protected or public). If you have automatic get

ters and set

ters for properties (i.e. Only get;

and set;

), they can be on the same line, otherwise on separate lines (if there is more code). Always include fields that begin with lowercase az, not underscores. The brackets should be on the new line. Always reference non-static members (i.e. Properties, Methods, Fields) withthis.

to distinguish them from variables and avoid ambiguity.



There is a huge list, but they are most relevant to your examples. Have a look at code.msdn.microsoft.com/sourceanalysis

And what you call global in your question is actually a "field". They should never be exposed (as I said above) because you are exposing your implementation, when in reality your behavior is all you have to expose on the interface to the type. Properties let you specify an interface, and even though they are now implemented as automatic properties, you can change the get

ters tags set

later without changing the interface.

+5


source


I believe code conventions should follow these rules:

  • Contribute to readable code.
  • If you work in a team, do whatever the team agrees.
  • If you are working alone, do what makes you feel warm and fuzzy inside.

Otherwise ... it doesn't really matter unless the rules are imposed on you by a higher power.

I personally prefer the camelCase variation and prefix private variables with m _.



Also, on personal projects that I don't expect other people to ever see this code, I like to use non-sequitur humor in my variable names i.e.

StringBuilder duck = new StringBuilder(4096);
duck.Append(palmtree.ToString());

      

It makes me laugh later.

+2


source


I recommend Microsoft Naming recommendations:

http://msdn.microsoft.com/en-us/library/ms229002.aspx

Interestingly, they don't talk about how to name the private attribute hidden behind the public property. I prefer the underscore prefix.

In general, CamelCase and lowercase letter first if it is a parameter.

+2


source


In addition to what the sniper said, I would add to be consistent.

You can also check General Naming Conventions on MSDN.

+1


source


Global: Starts with capital

Private: Runs with lowercase

Internal variables : starts with _.

This is what I read from a book a few years ago. This is the way programmers use it.

0


source


Another solid resource for C # naming conventions is the IDesign Coding Standard . This is pretty much consistent with the StyleCop / Microsoft coding standard with minor differences. Both agree that local variables and method arguments should have a camel, as in

    void MyMethod(int someNumber)  
    {int anotherNumber = 1;}

      

All in all I haven't seen much variation on this question, most people seem to follow this convention.
As for the private variables / fields, the more variances. StyleCop protects camel body, Juval protects m_MyPrivateVariable format. I've also seen people use _MyPrivateVariable.

0


source


For types, methods and properties, you use PascalCase (capitalization starts, uppercase at the beginning of dictionary blocks)

For variables, you always use camelCase (starts lowercase, uppercase at the beginning of dictionary blocks).

If the variable is a membervariable, some use the beginning of the name with an underscore _ to distinguish between a member variable and a local variable, but you can use the "this" facility for that too, just a matter of taste.

0


source







All Articles