Skip to content

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.

A C# program consists of statements, expressions and declarations. Variables store values; identifiers make those values understandable. Start here before learning classes.

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.

Example.cs
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.

Example.cs
int width = 8, height = 6;
var area = width * height; // inferred as int
Console.WriteLine($"Area: {area}");

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.

Example.cs
const int MaximumLives = 3;
int lives = MaximumLives;
lives--; // valid; MaximumLives itself cannot change
Console.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.

Program.cs
int score = 10; // Declaration and initialization
score = score + 5; // Assignment: 15
bool passed = score >= 12; // Comparison produces a bool
Console.WriteLine(passed); // True

Scope 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 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.

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.

Use meaningful identifiers for every value whose purpose is not immediately obvious. Keep variables in the smallest scope that needs them.

A variable declared inside an if block is not accessible outside that block. var x = null cannot infer a type.

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.