Ben vorig jaar vooral met Ruby en Rails bezig geweest, en nu werkzaam als .Net Developer in C#.

Linq extenties bieden vanaf C# 2008 een hoop leuke functies zoals Select, Aggregate, etc.  Eigenlijk alles om het werken met allerlei collecties behoorlijk te vergemakkelijken, vooral in combinatie met lambda expressies.

Maar 1 ding zat me dwars: waarom is er geen alternatieve foreach lus zoals in Ruby.

Voorbeeld:

collection = ["hello", "world"]

collection.each do |item|

puts item

end

Dit doet hetzelfde als deze C# code:

string[] collection = {”Hello”, “World”};

foreach (string item in collection)

{

Console.WriteLine(item);

}

Daarom heb ik uit nieuwsgierigheid dit proberen te bereiken in C# aan de hand van een eigen extensie en het is eigenlijk behoorlijk kort en eenvoudig om tot een erg praktisch resultaat te komen die werkt op alle standaard .NET collecties, arrays, etc…

De .cs-file: EachIteratorExtention

using System;
using System.Collections.Generic;
using System.Linq;

namespace EachExtensionExample
{
    public static class EachIteratorExtension
    {
        public static void Each<TSource>(this IEnumerable<TSource> source, Action<TSource> iterator)
        {
            foreach (TSource item in source) iterator(item);
        }
    }

    internal class Program
    {
        private static void Main()
        {
            string[] collection = { "Hello", "World" };
            collection.Each(item => Console.WriteLine(item));

            Console.WriteLine();

            //of meerdere lijnen
            collection.Each(item =>
            {
                Console.WriteLine(item);
                Console.WriteLine(item.ToUpper());
            });

            Console.WriteLine();

            //maar hetzelfde werkt met zowat alle .NET collecties
            //enige vereiste is dat ze IEnumerable implementeren
            var dates = new List<DateTime>();
            Enumerable.Range(0, 7).Each(day => dates.Add(DateTime.Now.AddDays(day)));
            dates.Each(d => Console.WriteLine(d.DayOfWeek));

            var dates2 = new List<DateTime>() { DateTime.Now };
            var nestedList = new List<List<DateTime>> { dates, dates2 };
            nestedList.Each(list =>
                Console.WriteLine("Dates in list: {0}", list.Count)
                );

            Console.ReadLine();
        }
    }
}

Edit: Kleine update gedaan, het is zelfs niet nodig van een delegate te gebruiken.

Posted in Uncategorized at December 15th, 2008. 3 Comments.

This week was the first week of the “project weeks”, 3 weeks to go. In a group of 3 students we have to finish the project they give us.

In short, our project is a browser-based meal-application for a school.
They are using a very old DOS application right now and they really need an update.

Everybody works on the same project, using different languages and frameworks. The best implemantion will be used in the school. And I’m lucky: using Ruby On Rails.
After 1 week we already have most of the functionality and a layout finished.

It’s kind of getting used to Rails 2, more things have changed than I though. Also I stopped using Eclipse as my IDE and now just use gEdit and the commandline.
Why? It’s fast and I’m learning a whole lot more without an IDE helping me all the time. So far I haven’t even missed a visual debugger, hope it stays this way…

Posted in Uncategorized at February 15th, 2008. 1 Comment.