Chapter 2: Language Basics
This chapter explores the small building blocks that make C# code readable, structured, and precise. You will see how the language divides text into tokens, how identifiers are formed, and how keywords shape control flow. These pieces form the grammar of the language and give you the vocabulary that later chapters expand upon.
Tokens, identifiers, and keywords
Every C# program begins life as a stream of characters. The compiler groups these characters into tokens which are the smallest meaningful parts of the language. Tokens include names, operators, literals, punctuation symbols, and keywords. You can imagine them as the tiles of a mosaic that the compiler later arranges into deeper meaning.
Identifiers are names you create for variables, methods, classes, and anything else you define. They can contain letters, digits, and underscores although they cannot begin with a digit. Keywords are reserved words such as if, class, return, and using which carry fixed meanings. They cannot be used as identifiers unless you prefix them with @ which allows special cases such as @class when needed.
Statements and expressions
A statement tells the program to perform an action. Common examples include variable declarations, assignments, loops, and method calls. Statements usually end with a semicolon except for compound statements which are grouped inside braces.
An expression produces a value. For example, 2 + 3 is an expression that yields the number five. Some expressions also have side effects such as modifying a variable. When a statement contains an expression, the compiler evaluates the expression first and then applies the meaning of the surrounding statement.
You can think of statements as instructions and expressions as calculations. Both work together to describe the behaviour of your program.
Variables, constants, and scope
Variables hold values that may change during the lifetime of a program. You declare them with a type and a name such as int count. Constants are values that cannot change once assigned and are declared using the const keyword. This helps prevent accidental modification and makes the intent clear.
Scope defines where a name is visible. A variable declared inside a block is available only within that block while a variable declared at the top of a class is available to all members of that class. Good scoping keeps code clean and makes accidental shadowing less likely.
Literals and string interpolation
Literals represent fixed values such as numbers, characters, strings, and boolean values. They appear directly in your code. For example, 42, "hello", and true are all literals. Numeric literals can include separators such as 1_000_000 to improve readability.
String interpolation allows you to embed expressions inside strings using the $ prefix. This technique makes it easy to compose meaningful output without manual string concatenation. For example, $"The total is {total}" inserts the value of total into the string at runtime.
Nullable reference types
Nullable reference types help you avoid unexpected null values. When this feature is enabled, all reference types are non null by default which means the compiler warns you if you forget to initialise something. If a reference can legitimately hold null, you mark it with ?. For example, string? note means the variable may or may not contain a valid string.
This feature encourages cleaner design and clearer intent. It shifts a common source of runtime errors into the compile phase where mistakes are easier to catch.
Coding style and naming conventions
C# has a widely followed style that aims to keep code predictable. Method names, property names, and class names use PascalCase while variables and parameters use camelCase. Braces typically appear on their own line. Indentation uses spaces rather than tabs although the number of spaces depends on project settings.
Consistent naming builds trust in the structure of the code. When readers recognise patterns they no longer need to guess. This frees the mind to focus on behaviour rather than formatting. Many teams rely on built in analyzers to enforce style rules which helps large projects remain tidy as they grow.