naming guidelines for variables

J

John Salerno

Does Microsoft have any naming guidelines for variables? I've been
reading their guidelines for just about every other type (methods,
parameters, properties, etc.) but nothing about variables. Are variables
treated the same as parameters (i.e., camel case)?

I notice that Hungarian notation is being done away with.

Also, is it good practice to use an underscore to begin the name of a
field? I've seen that, and it seems like a good idea, but I didn't know
if that wasn't part of the suggested guidelines either.

Thanks,
John
 
J

Joshua Flanagan

Microsoft has only provided naming guidelines for your public API -
which would be your public classes, interfaces, methods, etc. Local
variables, private fields, and even private methods and classes can be
named however you want. The naming guidelines are not to make your
internal source code more readable, they are intended to make your
consumer's source code more readable, by making your API consistent with
other .NET components.

There is no Microsoft recommended guideline (that I have seen) regarding
private variables or fields. Some people like an underscore prefix,
some like "m_", some just prefer using the same name as the public
property, just camelCased, instead of PascalCased (and prefixed with
"this." to avoid conflict with a similarly named method parameter).
Bottom line is that the decision is up to you. Decide on a standard
that you and your team are comfortable with and stick to it.
 
J

James Curran

John said:
Also, is it good practice to use an underscore to begin the name of a
field? I've seen that, and it seems like a good idea, but I didn't
know if that wasn't part of the suggested guidelines either.

It's not necessarily good or bad. If it helps you, it's fine.

As a former C++ programmer, it bothers me, as a user-defined name
beginning with an underscore would be illegal according to the C++ Standard,
but there's no reason why that should hold sway in C# code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top