Collections & a First Taste of LINQ

Course 1 · Ch 8
Collections & a First Taste of LINQ
C# had this idea in 2007 — seven years before java2-4's own Streams API

Fundamentals closes with C#'s everyday collections — and a first look at the feature that touches nearly every one of them, deferred to Course 2's own full chapter, but too central to leave out entirely here.

List<T> — A Growable Collection

List<string> names = new(); names.Add("Alice"); names.Add("Bob");

Nearly identical in behavior to java1-8's own List/ArrayList — grows automatically, generic. C#'s generics are reified rather than erased, a real difference Course 2's own Generics In Depth chapter covers in full; this chapter stays at the surface level.

Dictionary<TKey, TValue>

Dictionary<string, int> ages = new(); ages["Alice"] = 30; // indexer syntax — no .put() call needed Console.WriteLine(ages["Alice"]);

Comparable to java1-8/java2-2's own Map/HashMap — key-value pairs, average constant-time lookup. C#'s indexer syntax (ages["Alice"]) reads slightly more concisely than Java's explicit .put()/.get() calls, though the underlying idea is identical.

Iterating — foreach

foreach (var name in names) { Console.WriteLine(name); }

Directly comparable to Java's own enhanced for loop — same purpose, different keyword.

A First Taste of LINQ

int[] numbers = { 1, 2, 3, 4, 5, 6 }; // method syntax var evenSquares = numbers.Where(n => n % 2 == 0).Select(n => n * n); // query syntax — SQL-like, built directly into the language grammar var evenSquares2 = from n in numbers where n % 2 == 0 select n * n;

LINQ — Language Integrated Query — shipped in C# 3.0, back in 2007, genuinely comparable to java2-4's own Streams API but arriving seven years earlier. Another "C# first" moment, like csharp1-3's own switch expressions. LINQ offers two syntaxes for the same underlying query: fluent method chaining (.Where().Select()), and a genuinely unique SQL-like query syntax baked directly into C#'s own grammar — something neither Java nor most other languages on this site offer as native syntax at all.

Why Preview LINQ Now

LINQ works over anything implementing IEnumerable<T> — which includes List<T>, Dictionary<TKey,TValue>, arrays, and every collection this chapter just introduced. That's exactly why it belongs here rather than waiting entirely for Course 2 — it's the natural next step for everything just covered, not a separate, unrelated topic.

AspectJava Streams (java2-4)C# LINQ
ArrivalJava 8 (2014)C# 3.0 (2007) — first
Fluent chaining.filter().map().collect().Where().Select()
SQL-like query syntaxnot availablefrom...where...select, built into the grammar
Method syntax composes; query syntax reads naturally for SQL-like filtering
Method syntax chains cleanly with everything else in C#; query syntax often reads more naturally for filtering/joining operations that already feel SQL-like. Course 2's own LINQ In Depth chapter covers when to reach for each.
This is only a first taste — the real depth is Course 2's
Deferred execution, the difference between IEnumerable<T> and IQueryable<T>, and LINQ's full operator vocabulary are all Course 2 material — this chapter only establishes that LINQ exists and roughly what it looks like.

Coding Challenges

Challenge 1

Create a List<int> of at least six numbers, then use LINQ method syntax to filter for numbers greater than 10 and print the result using foreach.

📄 View solution
Challenge 2

Create a Dictionary<string, int> of at least four name/age pairs, then write the same query twice — once using LINQ method syntax and once using query syntax — to find everyone with age 30 or older, printing both results to show they match.

📄 View solution
Challenge 3

Write a short comment explaining why LINQ can operate over a List<T>, a Dictionary<TKey,TValue>, and a plain array all with the same syntax, referencing IEnumerable<T> directly.

📄 View solution

Chapter 8 Quick Reference — Course 1 Complete

  • List<T> and Dictionary<TKey,TValue> behave comparably to java1-8/java2-2's List and Map
  • foreach is directly comparable to Java's enhanced for loop
  • LINQ shipped in C# 3.0 (2007), seven years before java2-4's own Streams API
  • LINQ offers both fluent method syntax and a genuinely unique SQL-like query syntax built into the language grammar
  • LINQ works over anything implementing IEnumerable<T> — lists, dictionaries, and arrays alike
  • C# Fundamentals is now complete. Course 2 (Intermediate/Advanced) begins with Generics In Depth — C#'s reified generics, a genuine opposite design choice from java2-1's own type erasure.