Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
GimmeJob
Sign in
Programming · C# · Chapter 01 / 10

Programming learning path

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 reference

Conditions 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

  • var is 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.
  • foreach is the normal way to enumerate a sequence when you do not need manual indexing.

Source registry

2 chapter references
A tour of C#

Core language syntax, control flow, methods, classes and general C# semantics

Microsoft Learn · Official documentation
Source ↗
The C# type system

Value/reference types, nullability, conversions and type-system fundamentals

Microsoft Learn · Official documentation
Source ↗