My name is Edward Tanguay. I'm an American software and web developer living and working in Berlin, Germany.
yesterday: Inspiring ted talk: Sugata Mitra: The child-driven education: "any teacher who can be replaced by a machine, should be": http://is.gd/eZRvi.
yesterday: Always so painful to look up the German article of a borrowed IT word: der Framework or das Framework? LEO won't tell me: http://is.gd/eZMeU.
yesterday: I know what podcast I'm listening to tomorrow on my way to work: John Resig on technometria: http://is.gd/eZJfq.
yesterday: C# CODE EXAMPLE: Extension method to sort a generic collection of objects: http://is.gd/eZG6n.
yesterday: C# CODE EXAMPLE: A simple class that represents a matching quiz item: http://is.gd/eZFZV.
yesterday: After-work 13K, two 5Ks under sub-four marathon pace: 23:27, 27:27, legs feel great: http://tanguay.info/run.
yesterday: 6 yr old daughter's last 2 questions before falling asleep tonight: 1) Why are there humans? 2) Is there anything that doesn't have a name?
3 days ago: If you are a developer in Berlin and need to improve your English, I'm looking for groups to teach after work: http://tanguay.info/itenglish.
3 days ago: As far as I'm concerned, the singularity is already here, every time I wake up twitter tells me something amazing was created while I slept.
3 days ago: We're not suffering from information overload, we're suffering from faulty filtering.
3 days ago: Classic literature for free as nicely formatted 1-page or 2-page PDF downloads: http://www.planetebook.com/free-ebooks.asp.
C# CODE EXAMPLE created on Monday, February 22, 2010 permalink
How to check for first and last items in a foreach loop
This was one of the the many interesing ideas for avoiding index counter messiness in foreach loops. This reads nicely for short lists, e.g. dropdown/radiobutton collections, etc. but it is not resourceful for large lists (because .last() iterates through the collection) and it assumes all values are unique.
using System;
using System.Collections.Generic;
using System.Linq;

namespace TestForEach23433
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> names = new List<string> { "Smith", "Jones", "Rogers", "Anderson" };

            var first = names.First();
            var last = names.Last();
            foreach (var name in names)
            {
                Console.Write(name);

                if (name == first)
                    Console.WriteLine(" (first)");
                else if (name == last)
                    Console.WriteLine(" (last)");
                else
                    Console.WriteLine();
            }

            Console.ReadLine();
        }
    }
}
Julian Dominguez: That could be extremely inefficient (specially in large collections), as names.Last() automatically iterates the whole collection to get the value.
Klaus: Also if there are two identical values, one of which is the last, this will not work.
Edward: You're right. I was surprised there's not a cleaner way to do this other than (1) foreach/index, or (2) for loop, or (3) hard-to-read lambda code. I guess actually using a for loop would be the cleanest way to do this, although I like the way foreach makes code so readable.
need markup?