0
0
C Sharp (C#)programming~10 mins

Custom LINQ extension methods in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom LINQ extension methods
Define static class
Write extension method
Use 'this' keyword on first parameter
Call method on IEnumerable object
Method executes on each element
Return new IEnumerable or result
Use result in code
You create a static class with a static method that extends IEnumerable<T> using 'this'. Then you call it like a normal LINQ method on collections.
Execution Sample
C Sharp (C#)
public static class MyLinqExtensions {
  public static IEnumerable<int> EvenNumbers(this IEnumerable<int> source) {
    foreach(var num in source) if(num % 2 == 0) yield return num;
  }
}

var nums = new List<int>{1,2,3,4};
var evens = nums.EvenNumbers();
This code defines an extension method EvenNumbers that filters even integers from a list.
Execution Table
StepActionInputConditionOutput/Result
1Call EvenNumbers on nums[1,2,3,4]N/AReturns IEnumerable<int> (deferred)
2Start foreach in EvenNumbers11 % 2 == 0? FalseNo yield
3Next foreach22 % 2 == 0? Trueyield return 2
4Next foreach33 % 2 == 0? FalseNo yield
5Next foreach44 % 2 == 0? Trueyield return 4
6End foreachN/ANo more elementsIteration ends
7Use evens in codeIEnumerable<int>N/ASequence: 2, 4
💡 All elements processed; method yields only even numbers.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
numN/A1234N/AN/A
evens (deferred)N/AIEnumerable<int>IEnumerable<int>IEnumerable<int>IEnumerable<int>IEnumerable<int>[2,4]
Key Moments - 3 Insights
Why does EvenNumbers return IEnumerable<int> but not a list immediately?
Because the method uses 'yield return', it creates a deferred sequence. The actual filtering happens when you iterate over the result, as shown in steps 2-6.
How does the 'this' keyword make EvenNumbers an extension method?
The 'this' keyword before the first parameter tells C# this method extends IEnumerable<int>. So you can call it like nums.EvenNumbers(), as in step 1.
What happens if the input list is empty?
The foreach loop in EvenNumbers won't run, so no elements are yielded. The output will be an empty IEnumerable, similar to step 6 but immediately.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'num' at Step 4?
A2
B4
C3
D1
💡 Hint
Check the 'num' variable column in variable_tracker after Step 4.
At which step does the method yield the first even number?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Output/Result' column in execution_table for when 'yield return' happens.
If the input list was empty, how would the execution table change?
AMethod would yield all numbers anyway
BSteps 2-5 would be skipped; method ends immediately
CMethod would throw an error
DMethod would yield one element
💡 Hint
Refer to key_moments about empty input behavior.
Concept Snapshot
Custom LINQ extension methods:
- Define a static class
- Write static method with 'this' on first param
- Method extends IEnumerable<T>
- Use 'yield return' for deferred execution
- Call like normal LINQ method on collections
Full Transcript
This visual trace shows how to create and use a custom LINQ extension method in C#. We define a static class with a static method called EvenNumbers that extends IEnumerable<int> using the 'this' keyword. The method filters even numbers using a foreach loop and 'yield return' to produce a deferred sequence. When called on a list of numbers, the method does not immediately return a list but an IEnumerable that yields even numbers as you iterate. The execution table walks through each step of the foreach loop, showing which numbers are checked and which are yielded. The variable tracker shows how the 'num' variable changes each iteration and how the 'evens' variable holds the deferred sequence until iteration. Key moments clarify why 'yield return' causes deferred execution and how the 'this' keyword enables extension method syntax. The quiz tests understanding of variable values at steps, when yields happen, and behavior with empty input. This helps beginners see exactly how custom LINQ extension methods run step-by-step.