WP7 - OrderedDictionary / alternatives?

I'm completely new to C# so I'm about to make a horrible attempt at my own version of an OrderedDictionary unless someone can suggest an alternative.

I need to be able to access my elements by array index retaining the order they were added and I also will be frequently updating individual elements using their key.

1) Is there a collection that allows this on the phone?

2) If I keep a List and Dictionary will they both be pointing to the same item or is there some kind of pointer thing I have to do?:

Item i = new Item();
list.Add(i);
dict.Add( "key", i);

Answers


Using a List and a Dictionary is probably a good option actually. The "pointer thing" that you're talking about happens by default for Objects in .NET (any class and/or structure). All objects in .NET are passed around by reference.

So, if you use:

Item i = new Item();
list.Add(i);
dict.Add("key",i);
Console.WriteLine(list.Last() == dict["key"]);

Your output will be "true".

Best of luck!


I won't suggest using OrderedDictionary, since it's a non-generic container.

However, if you just want to use it like always. You can port Mono's version of OrderedDictionary.

https://github.com/mono/mono/blob/master/mcs/class/System/System.Collections.Specialized/OrderedDictionary.cs

Here's some tips if you want to port this:

  1. Remove any unavailable interface
  2. Remove serialization-related code
  3. Replace ArrayList with List<object>
  4. Replace Hashtable with Dictionary<object, object>

Here's my implementation (comes from the open source OpenNETCF Extensions library):

public class OrderedDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
{
    private Dictionary<TKey, TValue> m_dictionary;
    private List<TValue> m_list = new List<TValue>();
    private object m_syncRoot = new object();

    public OrderedDictionary()
    {
        m_dictionary = new Dictionary<TKey, TValue>();
    }

    public OrderedDictionary(IEqualityComparer<TKey> comparer)
    {
        m_dictionary = new Dictionary<TKey, TValue>(comparer);
    }

    public void Add(TKey key, TValue value)
    {
        lock (m_syncRoot)
        {
            m_dictionary.Add(key, value);
            m_list.Add(value);
        }
    }

    public TValue this[int index]
    {
        get { return m_list[index]; }
    }

    public TValue this[TKey key]
    {
        get { return m_dictionary[key]; }
    }

    public int Count 
    {
        get { return m_dictionary.Count; } 
    }

    public Dictionary<TKey, TValue>.KeyCollection Keys 
    {
        get { return m_dictionary.Keys; } 
    }

    public Dictionary<TKey, TValue>.ValueCollection Values 
    {
        get { return m_dictionary.Values; } 
    }

    public void Clear()
    {
        lock (m_syncRoot)
        {
            m_dictionary.Clear();
            m_list.Clear();
        }
    }

    public bool ContainsKey(TKey key)
    {
        return m_dictionary.ContainsKey(key);
    }

    public bool ContainsValue(TValue value)
    {
        return m_dictionary.ContainsValue(value);
    }

    public void Insert(int index, TKey key, TValue value)
    {
        lock (m_syncRoot)
        {
            m_list.Insert(index, value);
            m_dictionary.Add(key, value);
        }
    }

    public void Remove(TKey key)
    {
        lock (m_syncRoot)
        {
            if (ContainsKey(key))
            {
                var existing = m_dictionary[key];
                m_list.Remove(existing);
                m_dictionary.Remove(key);
            }
        }
    }

    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
    {
        return m_dictionary.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

Need Your Help

Set y axis label position ggplot2

r plot ggplot2

I am looking for graphic help using ggplot2.

@register.filter in my code

python django

from django import template

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.