Brad Wilson <[EMAIL PROTECTED]> wrote:

> Dictionaries of identical size with identical items will enumerate
> identically.

That's not true. The common strategies that hash tables use to resolve
conflicts, i.e. chaining, re-hashing and probing, all have dependencies
on the order of insertion. For example, the following program prints out
two different sequences:

---8<---
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

class App
{
    static void Main()
    {
        Dictionary<string,int> dict = new Dictionary<string,int>();
        
        // Add in order
        for (int i = 1; i <= 10; ++i)
            dict.Add(i.ToString(), i);
        Console.WriteLine(ToString(dict, ", "));
        
        // Add in reverse order
        dict.Clear();
        for (int i = 10; i >= 1; --i)
            dict.Add(i.ToString(), i);
        Console.WriteLine(ToString(dict, ", "));
    }
    
    static string ToString(IEnumerable items, string separator)
    {
        StringBuilder result = new StringBuilder();
        foreach (object item in items)
            result.Append(item).Append(separator);
        if (result.Length > 0)
            result.Length -= separator.Length;
        return result.ToString();
    }
}
--->8---

-- Barry

-- 
http://barrkel.blogspot.com/

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to