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.
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.
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.
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.
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.
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.
abstract | as | base | bool | break |
byte | case | catch | char | checked |
class | const | continue | decimal | default |
delegate | do | double | else | enum |
event | explicit | extern | false | finally |
fixed | float | for | foreach | goto |
if | implicit | in | int | interface |
internal | is | lock | long | namespace |
new | null | object | operator | out |
override | params | private | protected | public |
readonly | ref | return | sbyte | sealed |
short | sizeof | stackalloc | static | string |
struct | switch | this | throw | true |
try | typeof | uint | ulong | unchecked |
unsafe | ushort | using | using static | virtual |
void | volatile | while |
You don’t have to memorize any of the keywords. 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.
add | alias | ascending |
async | await | by |
descending | dynamic | equals |
from | get | global |
group | into | join |
let | nameof | on |
orderby | partial (type) | partial (method) |
remove | select | set |
value | var | when (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.
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.
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.
// 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.
// 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.
// valid naming
string i;
string cookie;
string _ninjas_are_among_us;
An identifier cannot have a blank space between multiple words.
// 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.
// 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.
// 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.
// 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#.
// 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.
namespace HelloWorld
{
// indented once
}
Between the curly braces we are inside the HelloWorld namespace so we indent one level.
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.
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 IDEs.
Formatting
Many programmers argue over where the first curly brace should be, on the same line as the statement or 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 same line as the function declaration.
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