Language basics, types & control flow
C# is a statically typed language. Every expression has a compile-time type, including local variables declared with var. var asks the compiler to infer the type; it does not make the variable dynamically typed.
Types and nullability
Built-in types such as int, double, bool, char and user-defined struct types are value types. Classes, arrays, delegates and string are reference types. The important distinction is assignment and identity semantics: assigning a value type copies its value; assigning a reference-type variable copies the reference to the same object.
Nullable value types use T?, for example int?. With nullable reference types enabled, string? communicates that null is expected, while string communicates that callers should treat the value as non-null.
string name = "Ada";
int? retries = null;
var score = 90; // inferred as int
string? note = null; // nullable referenceConditions and loops
C# uses if, switch, for, foreach, while and do. Pattern matching can also appear inside conditions and switch expressions.
if (score >= 80)
{
Console.WriteLine($"{name}: passed");
}
foreach (var value in new[] { 1, 2, 3 })
{
Console.WriteLine(value);
}Key points
varis compile-time type inference, not dynamic typing.- Value types and reference types differ primarily in copy/identity semantics.
- Nullable annotations describe intent and enable compiler warnings; they do not magically prevent every runtime null.
foreachis the normal way to enumerate a sequence when you do not need manual indexing.