views:

233

answers:

7

Hi

In a book I been reading they use a capital letter for 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 that way and like this way better but just wondering about stuff in the method.

So

public void MyMethod()
{
}

public string MyProperty {get; set;

}

and for private

private void myMethod()
{
}

But how about in the method?

like

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

Also how about if you have sort of a global variable like

public class Test
{
   private string bob;

   public Test()
   {
      bob = null;
   }
}

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

+1  A: 

I am of the opinion that code conventions should follow the following rules:

  1. Promote readable code.
  2. If you're working on a team, do whatever the team agrees to.
  3. If you're working alone, do whatever makes you feel warm and fuzzy inside.

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

Personally, I prefer the variation of camelCase and prefixing private variables with m_.

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

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

This makes me laugh later.

snicker
Why the downvotes?
snicker
+1  A: 

In addition to what snicker said, I'd add to be consistent.

You might also want to check out General Naming Conventions on MSDN.

Jay Riggs
+2  A: 

I recommend the Microsoft Naming Guidelines:

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

Interestingly, they don't broach the subject of how to name the private attribute hidden behind a public property. I tend to prefix with an underscore.

In general, CamelCase, and lowercase the first letter if it's a parameter.

Neil Kimber
They recommend not using prefixes for field names: http://msdn.microsoft.com/en-us/library/ms229012.aspx , and these guidelines are meant more for authors of class libraries and for the libraries' public methods and properties.
MusiGenesis
+5  A: 

Here are your code examples as they would be if they followed official Microsoft code style guidelines (enforced 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 from the spec: all fields should be private and lower case (except if they're constant). All methods should be Capitalised, whatever the access. If you want to expose a field (i.e. make it public or protected), use a property (which should be capitalized if it's protected or public). If you have automatic getters and setters for properties (i.e. just get; and set;), they can be on one line, otherwise on separate lines (if there's more code). Always name fields starting with lower case a-z, not underscores. Braces should be on a new line. Always reference non-static members (i.e. properties, methods, fields) with this. to distinguish them from variables and to avoid ambiguity.

There's a huge list but these are the most relevant to your examples. Look at code.msdn.microsoft.com/sourceanalysis

And what you call 'global' in your question is in fact a 'field'. These should never be exposed (as I said above) because you're exposing your implementation when in fact your behaviour is all you should expose on the interface to a type. Properties allow you to specify an interface and, even if they're implemented as automatic properties now, you can change the getters and setters later without changing the interface.

Joe
A: 

Global : Starts with Capital

Private : Starts with lower case

Inner variables : starts with _ .

This is what I read from a book years ago. It's the way programmers use.

Braveyard
"It's the way programmers use." - What does that mean?
Joe
I mean old school and modern programmers go through that way to define identifiers.
Braveyard
A: 

Another solid resource for C# naming conventions is the IDesign coding standard. It's largely in agreement with the StyleCop / Microsoft coding standard, with small differences. Both agree that local variables and method arguments should be camel-cased, as in

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

In general, I haven't seen many variations on that point, most people seem to follow that convention.
As far as private member variables / fields go, there is more variance. StyleCop advocates camel-casing, Juval the m_MyPrivateVariable format. I have also seen people use _MyPrivateVariable.

Mathias
A: 

For types, methods and properties you use PascalCase (starts uppercase, uppercase at start of wordparts)

For variables you always use camelCase (starts lowercase, uppercase at start of wordparts).

If the variable is a membervariable some people use start the name with an underscore _ to distinguish between member variable and local variable but you can also use the quelifier "this" for this, just a matter of taste.

codymanix