Monthly Archives: June 2011

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) {
		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) {
		} else {
			useGlue = true;
	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

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

Netduino Traffic Light

Last week I got my GHI FEZ Internet of Things Kit, which includes – besides many accessory parts – a .NET-Microframework-powered Arduino-like board, the Netduino.

I started playing around with both the software and hardware, and the first thing that really worked was a very simple traffic light. It displays the light sequence red, red/yellow, green, yellow and back to red which is used in Germany. Although it is not that spectacular I’ve uploaded a short video showing the hardware setup and the light sequence.

This is the very simple code behind it:

using System;
using System.Threading;
using GHIElectronics.NETMF.FEZ;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace NetduinoTrafficLights
    public class TrafficLightsDE
        public static OutputPort Red = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di1, false);
        public static OutputPort Yellow = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di0, false);
        public static OutputPort Green = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di3, false);

        public static void Main()
            while (true)






Over the next weeks and month I will play around with more of the kit’s accessory parts and I’ll continue posting code (and maybe videos) about that. If you like (or don’t like) the video, feel free to leave a comment.

The famous first entry

Actually, this blog was just created so no great content is available. Yet. ;)

If you like to read occasional articles about coding and related stuff, then please revisit soon or subscribe to the newsfeed of this blog.