IEnumerable<T> pipelines; deferred execution
Build LINQ pipelines over IEnumerable and see deferred execution vs materialization with ToList/ToArray.
IEnumerable<T> pipelines; deferred execution is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
LINQ pipeline idea
Pipeline = chain of operators that transforms a sequence.
- Source: an IEnumerable<T>
- Operators: Where, Select, etc.
- Sink: enumeration (foreach) or materialization (ToList)
Where + Select
Create a chain with Where then Select. The query runs as you iterate.
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
// Build pipeline: keep even numbers, then square them
var query = numbers
.Where(n => n % 2 == 0)
.Select(n => n * n);
foreach (int x in query)
{
Console.WriteLine(x); // 4, 16
}
}
}
Deferred execution effect
Deferred execution: the query is evaluated at enumeration time, so later changes to the source can be seen.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
List<int> data = new List<int>();
data.Add(1);
data.Add(2);
var evens = data.Where(x => x % 2 == 0); // query is not executed yet
data.Add(3);
data.Add(4); // modify source after building query
foreach (int e in evens)
{
Console.WriteLine(e); // prints 2 and 4 because execution is deferred
}
}
}
Materialization (ToList)
Use ToList/ToArray to materialize the sequence now and avoid later source changes.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
List<string> words = new List<string>();
words.Add("apple");
words.Add("banana");
var longOnes = words.Where(w => w.Length >= 6);
List<string> snapshot = longOnes.ToList(); // materialize now
words.Add("pear"); // changes after materialization
words.Add("cherry"); // length 6, but snapshot is already frozen
foreach (string w in snapshot)
{
Console.WriteLine(w); // prints "banana" only
}
}
}
OrderBy + Take
Combine OrderBy, Select, and Take/Skip to create small, readable pipelines.
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
int[] nums = new int[] { 5, 1, 4, 2, 3 };
var firstTwoSortedSquares = nums
.OrderBy(n => n) // 1,2,3,4,5
.Select(n => n * n)
.Take(2); // 1,4
foreach (int x in firstTwoSortedSquares)
{
Console.WriteLine(x);
}
}
}
Tips & gotchas
Tips:
- Queries build a plan; they run when you enumerate or materialize.
- Materialize with ToList when you need a stable snapshot.
- Keep each step small and named clearly.
- Avoid re-enumerating expensive sources; store a list if needed.
Deferred execution rule
Recap
Recap: Build small IEnumerable<T> pipelines with Where/Select/etc. Remember: execution is deferred until you iterate or call ToList/ToArray.
Frequently asked questions
Is the “IEnumerable<T> pipelines; deferred execution” lesson free?
Yes — the full text of “IEnumerable<T> pipelines; deferred execution” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “IEnumerable<T> pipelines; deferred execution”?
Build LINQ pipelines over IEnumerable and see deferred execution vs materialization with ToList/ToArray. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “IEnumerable<T> pipelines; deferred execution” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- IEnumerable pipelines; deferred execution
- Select, Where, OrderBy, Take/Skip
- Query vs method syntax