Category Archives: Project

New gadget: Arduino Uno

Just a short update: I recently received some fun stuff, including an Arduino Uno.

I’ve also been soldering some components together, and I’m really excited about getting into the Arduino Universe. So stay tuned for more updates on that topic.

As a first impression I shot a little video with two blinking LEDs (seen on the right side in the video):

C# Extension Methods, Part 3

While I’ll probably (have to) focus on Java for the next month, I just came up with the idea of just another extension method class. It is acually not very helpful but it again shows how powerful extension methods can be.

This is how it can be used:

StringBuilder buffer = new StringBuilder();
foreach (int i in 10.To(15))
{
    buffer.AppendLine("" + i);
}

I know that this piece of code does not look very helpful, because the same can easily be done using a simple for loop, but there might be some cases – especially when using LINQ – when it is quite useful to have an iterable range of int values.

The code is up on my github C# extension method project. Feel free to use or fork it.

nHangman – a tiny game engine (prototype)

Inspired by a question on StackOverflow (which was actually about using regex to replace strings used for a Hangman game) I hacked together a file in C# that contains a minimal implementation of the Hangman game.

The code is up on Github: https://github.com/jcoder/nHangman

It’s actually more of a protoype, but maybe it’s helpful to somebody.

Moving the extension methods project to github

Some time ago I started to write a class with some C# extension methods, adding more methods over time. I first uploaded the file to the webspace of my blog, but I think this is getting too complicated to mantain, which makes it harder than required to post new versions.

So I’ve put the code on github: https://github.com/jcoder/jCsharpExtensions

Feel free to stay updated there, fork it or just download it. If you have any comments or suggestions, please leave them in the comments below.

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.