Some stuff about Web and .NET development
RSS icon Email icon Home icon
  • Shuffling a collection using Fisher-Yates algorithm

    Posted on August 7th, 2010 Thibaut No comments

    Lately, I came across a situation where I needed to shuffle a collection. It’s the case when, for example, you need to display elements in a random fashion.

    This is where the Fisher-Yates algorithm comes into play. Created by 2 statisticians, this algorithm enables to generate a random permutation of a finite set and properly implemented, the shuffle is unbiased, so that every permutation is equally likely. Exactly what I was looking for !

    Below, you’ll find my C# implementation for shuffling a generic dictionary (adapting this to shuffle any other type of collection should be straightforward) :

    /// <summary>

    /// Shuffles a dictionary using the Fisher-Yates algorithm

    /// </summary>

    /// <typeparam name="TKey">Type of the key</typeparam>

    /// <typeparam name="TValue">Type of the value</typeparam>

    /// <param name="dictionary">Dictionary to shuffle</param>

    public static void Shuffle<TKey, TValue>(ref Dictionary<TKey, TValue> dictionary)


        KeyValuePair<TKey, TValue>[] keyValuePairs = dictionary.ToArray();

        Random random = new Random();


        for (int i = keyValuePairs.Length - 1; i >= 0; i–)


            int j = random.Next(0, i + 1);

            KeyValuePair<TKey, TValue> temp = keyValuePairs[i];

            keyValuePairs[i] = keyValuePairs[j];

            keyValuePairs[j] = temp;



        dictionary = keyValuePairs.ToDictionary(k => k.Key, k => k.Value);


    C ya’ ;)


  • C# HTML Email through Gmail

    Posted on May 2nd, 2009 Thibaut No comments
    Lately, I’ve been looking for a way of sending HTML through an email client, more precisely a Gmail account. And unfortunately, it seems that you cannot do this for now using the Gmail interface (if there is, just let me know). After some googling, I found an interesting C# WinForm program from Visual C# Kicks that enables you to send HTML mail through your Gmail account. You can download the zip of the project here.


  • Do not use List<T> in public signatures

    Posted on April 5th, 2009 Thibaut No comments

    The List<T> class is the generic equivalent of the ArrayList class. It implements the IList<T> generic interface using an array whose size is dynamically increased as required and can be useful in many contexts. But you should avoid using it in public signatures. Why is it so ?

    • The List<T> has too many members that makes it too heavy for many scenarios. Indeed, you might not want to return a List<T> collection with all its richness in all situations. According to some people, it’s even a violation of the Single Responsibility Principle
    • While the List<T> class is not unsealed, it’s not specifically designed to be extended. Indeed, the class has no protected and virtual methods to allow beheviour altering through inheritance.

    What should you use then ? It’s recommended that you use IList<T> or derived interfaces in public signatures.