C# Keywords & Identifiers (names) Tutorial

In this tutorial we learn about keywords in C# that have a special meaning to the compiler.

We also learn about custom names, called identifiers, and their naming and casing rules. Lastly we cover formatting and indentation.

Here's a table of contents of what you'll learn in this lesson:
(click on a link to skip to its section)

Let's jump right in.

What are keywords

Keywords are words that are reserved by C# because it means something specific to the language. These words may not be used as names for classes, methods, data containers etc. unless they have the @ symbol as a prefix.

We’ll explore most of these throughout the tutorial series, but here are a few that we’ve already seen in our HelloWorld program.

Example: string keyword
string message = "Hello World";

string is the keyword in this instance and means that message is a data container that contains a string of characters.

Example: void keyword
 void Main()

void is the keyword in this instance and here it means that the function called Main will not return a value.

We can’t use, for example, int as a name, the compiler won’t allow it.

Example: a keyword cannot be used as a name
string int = "Hello World"; // compiler error

But if we’re desperate, we can use the @ symbol in front of the keyword and it will work.

Example: bad practice
string @int = "Hello World";

Using a keyword as a name is usually considered bad practice, try to avoid it if possible.

Full list of keywords

The keywords in the table below are reserved identifiers in any part of a c# program.

abstractasbaseboolbreak
bytecasecatchcharchecked
classconstcontinuedecimaldefault
delegatedodoubleelseenum
eventexplicitexternfalsefinally
fixedfloatforforeachgoto
ifimplicitinintinterface
internalislocklongnamespace
newnullobjectoperatorout
overrideparamsprivateprotectedpublic
readonlyrefreturnsbytesealed
shortsizeofstackallocstaticstring
structswitchthisthrowtrue
trytypeofuintulongunchecked
unsafeushortusingusing staticvirtual
voidvolatilewhile  

You don’t have to memorize the keywords below. Throughout the tutorial series we will cover them in-depth.

Full list of contextual keywords

The keywords in the table below list contextual keywords that have special meaning only in a limited program context. They can be used as identifiers outside of that context.

addaliasascending
asyncawaitby
descendingdynamicequals
fromgetglobal
groupintojoin
letnameofon
orderbypartial (type)partial (method)
removeselectset
valuevarwhen (filter condition)
where (generic type constraint)where (query clause)yield

What is an identifier

An identifier is a name used to identify any user-defined item such as a variable or function. In our HelloWorld program an example would be the message data container.

Example: message is an identifier
string message = "Hello World";

In this case the word message is the identifier, the name of the data container.

Another example would be the Console.WriteLine() function.

Example: WriteLine is an identifier
Console.WriteLine(message);

In this case the word WriteLine is the identifier, the name of the function.

Identifier naming rules

In C# there are some rules regarding identifiers that we must remember.

C# does not allow punctuation characters such as $, %, # within identifiers.

Example:
// invalid naming
string #lostsock#thestruggleisreal;

string info@website.com;

string $noRefundsAllowed;

string ^_^;

Note that the @ symbol is allowed in front of an identifier.

C# does not allow an identifier to start with a number, but it may contain a number.

Example:
// invalid naming
string 21JumpStreet;

// valid naming
string mamboNumber5;

An identifier must start with a capital letter from A to Z, a lowercase letter from a to z or an underscore.

Example:
// valid naming
string i;

string cookie;

string _ninjas_are_among_us;

An identifier cannot have a blank space between multiple words.

Example:
// invalid naming
string My Computer;

// valid naming
string MyComputer;

string myComputer;

string my_computer;
Identifier casing rules

C# is a case-sensitive programming language, so words with lowercase letters are different from words with uppercase letters.

Example:
// even though they all use the word: learning
// each of these are different identifiers

string learning;

string Learning;

string LEARNING;

There are a few different types of casing used in programming.

Identifier casing conventions

There are some conventions for us to consider when writing our identifiers. These are not strict rules and is the individual programmer’s choice.

In camelCase, the first letter of the first word is lowercase, any subsequent words start with a capital letter. If there’s only one word, it starts with a lowercase letter.

Example: camelCasing
// camelCase
string myComputer;

string camel;

In PascalCase we capitalize the first letter of each word. If there’s only one word, it also starts with an uppercase letter.

Example: PascalCasing
// PascalCase
string MyComputer;

string Pascal;

In snake_case, all the words are lowercase with an underscore that separates them. If there’s only one word, it’s all lowercase.

Even though studies have proven this one to be the easiest to read, it’s one of the ones that’s the least used in C#.

Example: snake_case
// snake_case
string my_computer;

string snake;

Each language usually has it’s own conventions. Mostly, in C# camelCase is used for variables and constants, and PascalCase is used for classes and functions.

In C, for example, most people will write functions in camelCase. In Python on the other hand, all data containers and functions are written with snake_casing.

The type of casing you use is up to your personal preference, the important thing is to be consistent. You may also be forced to follow certain conventions set out by employers.

Code formatting and indentation conventions

To make our code easier to read we format and indent it a certain way, these are also conventions and don’t strictly need to be followed unless set out in a professional environment.

Indentation

The general rule is to indent one level for each new block of code. Between the curly braces we indent one level.

Example: level 1 indentation
namespace HelloWorld
{
    // indented once
}

Between the curly braces we are inside the HelloWorld namespace so we indent one level.

Example: level 2 indentation
namespace HelloWorld
{
    class Program
    {
        // indented twice
    }
}

Between the second pair of curly braces we are inside the new code block of the Program class so we indent a second level.

Example: level 3 indentation
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            // Indented three times
        }
    }
}

Between the third pair of curly braces we are inside the new code block of the Main function so we indent a third level

The amount of spaces you want to use for your indentation is up to you, the important part again is to be consistent. Two, four and eight spaces are common and studies have shown that a four-space indent makes the code most readable.

We use the four-space indent in this tutorial series which is the default setting for the horizontal Tab in most IDE’s.

Formatting

Many programmers argue over where the first curly brace should be, on the same line as the statement or on the next line.

Example: curly brace on the same line
static void Main(string[] args) {
    string message = "Hello World";

    Console.WriteLine(message);
    Console.ReadLine();
}

In the example above, the curly brace is on the same line as the function declaration.

Example: curly brace on the next line
static void Main(string[] args)
{
    string message = "Hello World";

    Console.WriteLine(message);
    Console.ReadLine();
}

In the example above, the curly brace is on the next line after the function declaration.

It comes down to preference, what feels natural to you. Perhaps you come from a Javascript background and write the curly brace on the same line, or like us, you come from C and C++ and write the curly brace on its own line.

It really doesn’t matter which you use, consistency is the key. In this tutorial series we will be using the style where the curly brace is on its own line.

Summary: Points to remember

  • Keywords are reserved words that cannot be used as identifiers
    • Contextual keywords can be used as identifiers in certain cases
  • An identifier is simply a name
    • Identifiers may not have punctuation characters
    • Identifiers may not start with a number, but may contain one
    • Identifiers may not have blank spaces
    • Identifiers are case sensitive
  • While we don’t have to follow identifier, formatting or indentation conventions, it is recommended