An enumeration of one?

Having something enumerable is the gateway to all LINQ’s loveliness.

But sometimes you have just one object.

So how do you make that enumerable?

var people = new Person[] {person};

Not exactly hard huh?

In fact you can simplify this even more, you can replace new T[] with just new [] like this:

var people = new [] {person};

And the compiler will infer the type of the array for you.

At this point you can start treating your object as a enumeration, and gain all the benefits of the collection monad, not least of which is delayed execution.

var results = from x in new [] {person}
                 select DoSomethingPrettyCPUIntensive(x);

And notice the expensive operation is only executed if someone actually enumerates the results.

Skip to main content