C# LINQ Background Topics

player_one
1,129 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

IEnumerable<T> - Unbounded generators

The Fibonacci generator

In the previous lesson, we looked at an IEnumerable<int> backed by a List<int>. An IEnumerable<T> could also be backed by a stateful method, rather than a static collection. This method is called a generator, and it looks like this:

private IEnumerable<int> GetFibonacci()
{
    int previousVal1 = 0;
    int previousVal2 = 1;

    while (true)
    {
        int nextVal = previousVal1 + previousVal2;
        previousVal1 = previousVal2;
        previousVal2 = nextVal;
        yield return nextVal;
    }
}

Wait a minute! What does that return? It looks like it's returning an int, even though the method declaration returns an IEnumerable<int>. And what's up with that infinite loop?

How it works

The magic is in that yield return statement. Within the context of a single iteration, each execution of that line returns the next value in the sequence. The yield return statement maintains context so that a request for the next value can continue execution right where it left off.

Be aware that, at least in this example, this method produces a pseudo-infinite sequence. It's not truly infinite, as will be seen in the next lesson.

Exercise

See if you can write your own generator method that implements the following sequence:

Alternate between adding 2 to the previous number, then multiplying the previous number by 2. Here are the first several values in this sequence:

  0+2, 2x2, 4+2, 6x2, 12+2, 14x2, 28+2, 30x2, 60+2 ...
= 2,   4,   6,   12,  14,   28,   30,   60,   62 ...
Unbounded Generator Exercise
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System.Collections.Generic;
namespace Generators1
{
public static class GeneratorsExercise1
{
// Write a method that implements a generator
// for the sequence of integers that follows
// the pattern prev + 2, prev x 2, prev + 2, etc.
//
// The method should be called AddTwoThenDouble()
// and should take no parameters.
// public static ... AddTwoThenDouble()
// {
// }
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content