concept IEnumerable in category c#

appears as: IEnumerable, IEnumerable, n IEnumerable
Code like a Pro in C#

This is an excerpt from Manning's book Code like a Pro in C#.

Welcome to the deep end. Listing 4.1 is a fair bit of code with a lot of things that may be new to us. All endpoints in the controller look similar to this one. They all use the same patterns to get the data and to return it, so after we fully understand what is happening here, the other endpoints are going to be be a breeze. In this section, we look at the method signature of the /flight’s Get method. We start by examining the ResponseType method attribute, followed by a discussion on the typeof keyword and what it does. Lastly, we glimpse how the ResponseType method attribute uses the IEnumerable interface and typeof operator. What does the method signature look like?

Functional Programming in C#: How to write better C# code

This is an excerpt from Manning's book Functional Programming in C#: How to write better C# code.

LINQ offers implementations for many common operations on lists (or, more generally, on “sequences,” as instances of IEnumerable should technically be called), the most common of which are mapping, sorting, and filtering (see the “Common operations on sequences” sidebar). Here’s an example combining all three:

Enumerable.Range(1, 100).
   Where(i => i % 20 == 0).
   OrderBy(i => -i).
   Select(i => $"{i}%")
// => ["100%", "80%", "60%", "40%", "20%"]

Notice how Where, OrderBy, and Select all take functions as arguments and don’t mutate the given IEnumerable, but return a new IEnumerable instead, illustrating both tenets of FP you saw earlier.

An implementation of Map for IEnumerable can be written as follows.

Listing 4.1. Map applies a function to each element of the given IEnumerable
public static IEnumerable<R> Map<T, R> (this IEnumerable<T> ts, Func<T, R> f) { foreach (var t in ts) yield return f(t); }

Map maps a list of T’s to a list of R’s by applying a function T → R to each element in the source list. Notice that in this implementation the results are packaged into an IEnumerable as a result of using the yield return statement.

Figure 4.1. Mapping a function over an IEnumerable

Remember that Map loops over a given IEnumerable and applies a function to each element. Bind is similar, but with a nested loop, because applying the “bound” function also yields an IEnumerable. The resulting list is flattened to a one-dimensional list. It’s probably easier to see this in code:

public static IEnumerable<R> Bind<T, R>
   (this IEnumerable<T> ts, Func<T, IEnumerable<R>> f)
{
   foreach (T t in ts)
      foreach (R r in f(t))
         yield return r;
}

If you’re intimately familiar with LINQ, you’ll recognize that this implementation is virtually identical to SelectMany. So for IEnumerable, Bind and SelectMany are identical. Again, in this book I’ll use the name Bind because it’s standard in FP-speak.

Once you’ve used Range to go from regular values to an IEnumerable<int>, all following computations stay within the IEnumerable abstraction. That is, staying within one abstraction gives you the ability to nicely compose several operations—something we’ll delve into in the next chapter.