Two kinds of rules
Naming a variable involves two separate sets of rules. The first set is enforced by the compiler: break one and the program does not build. The second is convention, which the compiler ignores but every C# reader expects. Both matter, because code is read far more often than it is written.
What the compiler requires
A variable name must follow these rules:
- It consists of letters, digits and underscores only.
- It starts with a letter or an underscore, never a digit.
- It contains no spaces or other symbols such as
.,-or#. - It is not a reserved keyword like
classorint.
These declarations are all rejected:
int 18spam = 1; // starts with a digit
string first name = "Kate"; // contains a space
bool #name = true; // contains a symbol
int class = 3; // reserved keyword
Names containing letters beyond the English alphabet are legal, so café compiles, and a keyword can be used as a name if you prefix it with @, as in @class. You rarely want either; they are just details you may run into.
Names are case-sensitive
C# treats upper and lower case as different characters. These are three separate variables:
string name = "Kate";
string Name = "Andy";
string NAME = "George";
Console.WriteLine(name + " " + Name + " " + NAME); // Kate Andy George
It compiles, but it is a bad habit. A reader, or you next month, will mix them up, and a typo in capitalisation silently picks the wrong variable. Never tell variables apart by case alone.
Notice how the line above prints all three values: it joins them with +. Passing them as separate arguments, Console.WriteLine(name, Name, NAME), does not do that. With a string as the first argument, WriteLine treats it as a format template. Since "Kate" contains no placeholders, only Kate is printed.
Conventions for multi-word names
Because names can't contain spaces, a name with several words needs a way to mark where one word ends. There are two common styles:
- camelCase: the first word is lowercase and each later word starts with a capital letter, as in
firstNameandnumberOfEmployees. - snake_case: words are separated by underscores, as in
first_name.
Both compile, but C# code uses camelCase for local variables and method parameters. Methods, classes and properties use PascalCase, where every word is capitalised, such as WriteLine or Console. Whichever style you pick, stick to one. Mixing first_name and lastName in the same program works but looks sloppy.
Boolean names
A variable holding true or false reads best as a yes/no question: isEmployed, hasLicense. Then if (isEmployed) reads almost like English.
Descriptive names
Short names like x are fine for quick, local use. For anything that matters, say what the value means. Compare:
int a = 25;
int b = 13;
int c = a * b;
int hours = 25;
int rate = 13;
int pay = hours * rate;
The compiler accepts both. Only the second tells you what is being calculated.
The exercise, step by step
- A string for the first name. The two words "first" and "name" become
firstName: first word lowercase, second word capitalised. Assign"John". - A bool for employment.
isEmployedfollows the yes/no pattern and is assignedtrue. - An int for experience.
yearsOfExperiencehas three words, so two capital letters. Assign5.
string firstName = "John";
bool isEmployed = true;
int yearsOfExperience = 5;
All three names use camelCase, so the code is consistent.
Common mistakes
- Wrong capital letters.
FirstName,firstnameandfirstNameare three different names, and only the last matches the lesson. - Starting with a digit.
2ndPlaceis invalid;secondPlaceworks. - Using a spaced name.
years of experienceis three tokens to the compiler, not one name.
Key takeaways
- Names use letters, digits and underscores, and can't start with a digit or be a keyword.
- Names are case-sensitive.
- Local variables use camelCase; keep one style throughout a program.
- Choose names that say what the value means.