Ask Learn Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
A software developer once famously said, "The hardest part of software development is naming things." Not only does the name of a variable have to follow certain syntax rules, it should also be used to make the code more human-readable and understandable. That's a lot to ask of one line of code!
There are some variable naming rules that are enforced by the C# compiler.
#
, the dash -
, and the dollar sign $
are not allowed.float float;
or string string;
.string MyValue;
and string myValue;
are two different variables.Conventions are suggestions that are agreed upon by the software development community. While you're free to decide not to follow these conventions, they're so popular that it might make it difficult for other developers to understand your code. You should practice adopting these conventions and make them part of your own coding habits.
string thisIsCamelCase;
.bool orderComplete;
, NOT bool isComplete;
.decimal orderAmount;
, NOT decimal odrAmt;
.string strMyValue;
. It was a popular style years ago. However, most developers don't follow this advice anymore and there are good reasons not to use it.The example string firstName;
follows all of these rules and conventions, assuming I want to use this variable to store data that represents someone's first name.
Here's a few examples of variable declarations (using common data types):
char userOption;
int gameScore;
float particlesPerMillion;
bool processedCustomer;
The rules and conventions described above are for local variables. A local variable is a variable that is scoped within the body of a method, or a variable in a console application that uses top-level statements (like the code in this module).
There are other types of constructs that you can use in your applications, and many have their own conventions. For example, classes are often used in C# programming, and have associated conventions. Although you won't be creating classes in this module, it's important for you to know that the naming conventions you just learned about fit into a larger naming framework.
Please sign in to use this experience.
Sign in