typestar

Language basics

29 steps in 9 sets of C#.

C# is what a language looks like after twenty-five years of steady editing. It launched in 2000 as Microsoft's answer to Java and has shipped a major revision roughly every year since, each one shaving ceremony off the last: top-level programs lost the boilerplate Main, records collapsed a forty-line class into one line, switch grew from a jump table into pattern matching. The result reads far lighter than its reputation suggests.

This tour covers the shared core — variables, strings, control flow, collections, methods, classes — in modern form, then the two things C# holds over its neighbors: records with value semantics, and LINQ, which turns loops over data into sentences about data. The encore ships two complete command-line programs. Everything here compiles under .NET's strict defaults before it reaches you.

Start this tour

Variables & types

Strings

  • String interpolationThe $ string is C#'s template: expressions, formats and alignment inline.
  • String methodsThe everyday string toolkit: trim, transform, search and slice.
  • Split & joinStrings to collections and back again.
  • StringBuilderStrings are immutable, so repeated concatenation copies; a builder does not.

Flow control

  • If & elseBranching: the if/else chain and the conditional operator.
  • Switch expressionsThe modern switch returns a value and matches patterns, not just constants.
  • Loopsfor counts, foreach walks, while runs until told otherwise.

Collections

  • ArraysFixed-size, typed, and sliceable with index-from-end and range syntax.
  • ListsThe resizable workhorse collection.
  • DictionariesKey to value in constant time, with a safe way to ask for what might be missing.
  • HashSetA collection that answers one question fast: have I seen this before?

Methods

Classes & records

  • Classes & propertiesAuto-properties and object initializers, the shape of everyday C#.
  • ConstructorsHow an object insists on the data it cannot exist without.
  • InheritanceOne base contract, many behaviors, chosen at run time.
  • RecordsValue semantics in one line: printing, equality and copies with a change.

LINQ

Errors & files

  • Try & catchCatch the exception you expect; let the rest travel.
  • FilesWhole-file reads and writes in single calls.
  • using & DisposeDeterministic cleanup: the file handle closes when the scope ends.

Encore

  • word_freq.csThe word-count CLI in C#: stream a file, tally a dictionary, report a top ten.
  • grade_report.csA gradebook in one file: averages, letter grades and a summary table.

The other C# tours