Tag Archives: Code-Download

C# Extension Methods, Part 2

I’ve just made another tiny addition to my extension method class / project, adding a small, but maybe useful method:

/// <summary>
/// Repeats this IEnumerable a given number of times
/// </summary>
/// <param name="enumerable">this IEnumerable</param>
/// <param name="times">number of times to repeat this IEnumerable</param>
/// <returns>IEnumerable</returns>
public static IEnumerable<T> Repeated<T>(this IEnumerable<T> enumerable, int times)
{
    if (times < 1)
    {
        yield break;
    }
    for (int i = 0; i < times; i++)
    {
        foreach (T oneObject in enumerable)
        {
            yield return oneObject;
        }
    }
}

This is how to use it to consume an IEnumerable<T> multiple times:

var range = Enumerable.Range(1, 5);
foreach (var i in range.Repeated(5))
{
    // doing something with i, which will be the sequence
    // from 1 to 5, repeated 5 times
}

It’s not a big deal, but maybe it’s helpful anyway. Btw, the source code is updated, too.

Continuous project: C# Extension Methods

There are a lot of cool features in the current C# version (and I’m sure that I will cover most of them in future postings), but one of the features I like most are Extension Methods.

If you’ve never heard about Extension Methods, let me cite the MSDN documentation on Extension Methods:

Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

Extension Methods are a very powerful feature when it comes to writing easy-to-understand code. So, over the course of the weeks and months I’ll post code fragments with (mostly) helpful Extension Methods, at least if they’re helpful for me. I will also upload the latest source code files to my blog.

Unless otherwise stated the code is provided as “Public Domain” which means that you can modify and use it in all your projects.

So, let’s get started with the first methods.

/// <summary>
/// Performs an action with each of the elements in this IEnumerable
/// </summary>
/// <param name="enumerable">this IEnumerable</param>
/// <param name="action">action to be performed on each element</param>
/// <returns>IEnumerable</returns>
public static IEnumerable<T> WithEach<T>(this IEnumerable<T> enumerable, Action<T> action) {
	if (action == null) {
		action = (obj) => {};
	}
	foreach (T oneObject in enumerable) {
		action(oneObject);
		yield return oneObject;
	}
}

/// <summary>
/// Concatinates all elements of this IEnumerable to a flat string, optionally quoting them
/// </summary>
/// <param name="enumerable">this IEnumerable</param>
/// <param name="glue">glue between the elements</param>
/// <param name="quoteStart">quote at start of each element</param>
/// <param name="quoteEnd">quote at end of each element</param>
/// <returns>flat string</returns>
public static string ToFlatString<T>(this IEnumerable<T> enumerable, string glue, string quoteStart, string quoteEnd) {
	if (glue == null) {
		glue = ",";
	}
	if (quoteStart == null) {
		quoteStart = "";
	}
	if (quoteEnd == null) {
		quoteEnd = "";
	}
	StringBuilder strBuilder = new StringBuilder();
	bool useGlue = false;
	foreach (T oneObject in enumerable) {
		if (useGlue == true) {
			strBuilder.Append(glue);
		} else {
			useGlue = true;
		}
		strBuilder.Append(quoteStart).Append(oneObject).Append(quoteEnd);
	}
	return strBuilder.ToString();
}

But wait, there’s more ;)

public static string ToFlatString<T>(this IEnumerable<T> enumerable, string glue)
public static string ToFlatString<T>(this IEnumerable<T> enumerable, string glue, Func<T, string> toStringFnc)

As don’t want this posting to be just code, I’ve only outlined the additional method signatures here, so feel free to download the complete file from http://blog.jcoder.me/files/csharp/IEnumerableExtensions.cs

If you have any questions or ideas for useful Extension Methods, please leave a comment below.