Posted by Mark Withall: 2012-11-24

Just to test out how the syntax highlighting works for posting blog entries, here’s a short piece of code that I wrote a while ago to partition a list into fixed-size blocks.

public static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> list, int size)
{
    while (list.Any()) { yield return list.Take(size); list = list.Skip(size); }
}

And for extra credit, here are some extensions to make it work for strings too.

public static IEnumerable<string> Partition(this string str, int size)
{
    return str.Partition<char>(size).Select(AsString);
}

public static string AsString(this IEnumerable<char> charList)
{
    return new string(charList.ToArray());
}

These were originally posted by me on stackoverflow.