Syntax, variables and constants
↑Examples are independent C# snippets for modern .NET console projects unless noted otherwise. Class declarations and top-level code can live in Program.cs when top-level statements come first.
At a glance
Section titled “At a glance”A C# program consists of statements, expressions and declarations. Variables store values; identifiers make those values understandable. Start here before learning classes.
How it works
Section titled “How it works”A small working program
Section titled “A small working program”A semicolon ends most statements. Curly braces group statements into blocks. With top-level statements, a console application can start without an explicit Main method. Console.WriteLine writes a line to standard output.
using System;
string playerName = "Alex";int score = 12;Console.WriteLine($"{playerName} scored {score} points.");Identifiers, declarations and multiple values
Section titled “Identifiers, declarations and multiple values”Identifiers are case-sensitive: score and Score differ. Prefer descriptive camelCase for local variables and PascalCase for types and public members. You can declare multiple variables of one type, but separate declarations are often clearer. var asks the compiler to infer a static compile-time type; it is not the same as dynamic.
int width = 8, height = 6;var area = width * height; // inferred as intConsole.WriteLine($"Area: {area}");Constants and displaying values
Section titled “Constants and displaying values”Use const for a value known at compile time and fixed for every caller. Ordinary variables can be reassigned. Prefer meaningful names instead of scattering literal numbers through calculations. For values determined during execution, see readonly in the static and immutability lesson.
const int MaximumLives = 3;int lives = MaximumLives;lives--; // valid; MaximumLives itself cannot changeConsole.WriteLine("Lives left: " + lives);Deep dive: read a C# program from top to bottom
Section titled “Deep dive: read a C# program from top to bottom”Declarations, assignments, expressions, and statements
Section titled “Declarations, assignments, expressions, and statements”int score = 10; declares a variable, gives it a type and initializes it. The 10 is a literal expression. Later, score = score + 5; is an assignment statement: C# first evaluates the right-hand expression using the old value and then stores the result. The name is not itself the number; it identifies a storage location with rules defined by its type and scope.
int score = 10; // Declaration and initializationscore = score + 5; // Assignment: 15bool passed = score >= 12; // Comparison produces a boolConsole.WriteLine(passed); // TrueScope and lifetime are different questions
Section titled “Scope and lifetime are different questions”Scope determines where a variable name can be used: a variable declared inside a { ... } block is generally not visible outside it. Lifetime is how long the underlying value or object is retained. These are related but not identical: an object may remain alive after a local variable referring to it goes out of scope if another reference still reaches that object.
var is still strongly typed
Section titled “var is still strongly typed”var score = 10; gives score the compile-time type int; a later assignment of a string fails. var is useful when the type is obvious or long, but an explicit type often makes the intent clearer for numeric conversions and public APIs. dynamic is different: it delays many member checks until runtime and can cause runtime binder errors.
Conventions protect future readers
Section titled “Conventions protect future readers”Use PascalCase for types, methods and public properties, camelCase for local variables and parameters, and descriptive names for constants. Names such as elapsedSeconds clarify units, which is especially important in movement, timing and game code. Avoid names that differ only by case or one character. Comments should document why an operation is performed; the variable name should already describe what it contains.
When and why
Section titled “When and why”Use meaningful identifiers for every value whose purpose is not immediately obvious. Keep variables in the smallest scope that needs them.
Common mistake
Section titled “Common mistake”A variable declared inside an if block is not accessible outside that block. var x = null cannot infer a type.
Try it yourself
Section titled “Try it yourself”Create a program with a name, a score and a named maximum score. Print whether the player reached the maximum.
Reveal a solution
const int MaxScore = 100; int score = 100; Console.WriteLine(score == MaxScore); prints True.
