Team LiB
Previous Section Next Section

Legal Variable Names

Before moving too far into C# syntax, it's important to understand variable naming in C#. C# is a case-sensitive language. Variable names that differ only by case are considered two different entities. The following declarations illustrate this point:

int wholePart = 10;
int WholePart = 20;

The first declaration of an integer named wholePart is considered unique based on case and does not conflict with the second declaration of WholePart. Although each variable is spelled the same, because they differ in case, C# considers them two separate entities. This is important to note if you are coming from a language such as Microsoft Visual Basic where case sensitivity is not an issue. In addition, C# specifies rules for how variables can be named. Legal names start with a letter or underscore followed by one or more letters, underscores, or digits. Listing 3.1 shows some legal and illegal variable declarations to illustrate the C# rules for variable names.

Listing 3.1. C# Variable Naming Rules
int 9abc;        //illegal. Starts with an integer
int _abc;        //legal starts with an underscore
int _9abc;       //legal starts with an underscore
int #myAge;      //illegal. Starts with an illegal character
int myAge_1;     //legal. Starts with a char followed by

    Team LiB
    Previous Section Next Section