Start learning free

LESSON 11 OF 347 · VARIABLES

Variable Naming Rules in C#: Valid Names and camelCase

Lesson Read the full explanation

TL ; DR:

  • In C#, we use the camelCase convention to name our variables. It implies separating different words with capital letters, like this:

    string companyName = "Apple";
    
    int foundingYear = 1976;
    
    bool isUnicorn = true;
    
    int numberOfEmployees = 137000;
    



Full lesson:

Variable names must consist ONLY of letters, numbers and underscores and they must start with a letter or underscore.

Bad variable names:

int 18spam;
string x.15;
bool #name;

If you try to create a variable with any of these names, C# will throw an error.


Naming conventions: camelCase and snake_case

Sometimes a variable name might consist of multiple words, for example if you want to store the first name of a person. You might be tempted to separate those names by whitespaces, like this:

string first name = "Kate";

This code will also throw an error.

When you name your variables, you want to use one of these two conventions:

snake_case = separate different words with an underscore (_), like this:

string first_name = "Jane";
string last_name = "Doe";

camelCase = separate different words with capital letters, like this:

string firstName = "Jane";
string lastName = "Doe";

It's important to be consistent with the casing technique you use. Do not combine both of them in the same code, like this:

string first_name = "Jane";
string lastName = "Doe";

Case sensitive:

Variable names are case sensitive:

string name = "Kate";
string Name = "Andy";
string NAME = "George";

Console.WriteLine(name, Name, NAME); // Output: Kate Andy George

Although C# allows us to do this, it's always confusing and a bad coding practice to differentiate variable names just by case!


Mnemonic Variable Names:

Since we programmers are given a choice in how we choose our variable names, there is a bit of "best practice".

Sometimes we want them short like x and y, but sometimes we want them descriptive, especially if they are important.

Here is a great example:

int a = 25;
int b = 13;
int c = a * b;
Console.WriteLine(c);

compared to:

int hours = 25;
int rate = 13;
int pay = hours * rate;
Console.WriteLine(pay);

C# understands and is happy with both of these pieces of code. But it's clearly which version makes your reader happier.


Assignment
Follow the Coding Tutorial and let's let's name some variables well!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    C# · VARIABLES

    Variable Naming Rules in C#, explained

    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 class or int.

    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 firstName and numberOfEmployees.
    • 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

    1. A string for the first name. The two words "first" and "name" become firstName: first word lowercase, second word capitalised. Assign "John".
    2. A bool for employment. isEmployed follows the yes/no pattern and is assigned true.
    3. An int for experience. yearsOfExperience has three words, so two capital letters. Assign 5.
    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, firstname and firstName are three different names, and only the last matches the lesson.
    • Starting with a digit. 2ndPlace is invalid; secondPlace works.
    • Using a spaced name. years of experience is 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.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free