2012-09-08 34 views
0

我對C#完全陌生,所以我打算在自己的OrderedDictionary版本上做一個可怕的嘗試,除非有人可以提出一個替代方案。WP7 - OrderedDictionary/alternatives?

我需要能夠通過數組索引來訪問我的元素,保留它們添加的順序,我也會經常使用它們的鍵來更新單個元素。

1)有沒有一個允許在手機上使用的集合?

2)如果我保持一個列表和字典將它們都被指向同一項目或有某種指針事情我必須做的?:

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

回答

0

使用列表和字典是實際上可能是一個好選擇。 .NET中的對象(任何類和/或結構)默認情況下,您正在討論的「指針事件」。 .NET中的所有對象都通過引用圍繞傳遞。

所以,如果你使用:

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

你的輸出將是 「真」。

祝你好運!

0

我不會建議使用OrderedDictionary,因爲它是一個非泛型容器。

但是,如果你只是想像往常一樣使用它。您可以移植Mono的OrderedDictionary版本。

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

下面是一些提示,如果你想將這樣的:

  1. List<object>
  2. 刪除任何可用的接口
  3. 刪除序列化相關的代碼
  4. 更換的ArrayList替換爲哈希表Dictionary<object, object>
0

這是我的實現(來自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(); 
    } 
}