0

我对 C# 完全陌生,所以除非有人可以提出替代方案,否则我将对我自己的 OrderedDictionary 版本进行一次可怕的尝试。

我需要能够通过数组索引访问我的元素,保留它们被添加的顺序,并且我还将经常使用它们的键更新单个元素。

  1. 是否有允许在手机上进行此操作的集合?

  2. 如果我保留一个列表和字典,它们会指向同一个项目还是我必须做某种指针的事情?:

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

3 回答 3

1

这是我的实现(来自开源 OpenNETCF 扩展库):

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();
    }
}
于 2012-09-08T17:09:15.340 回答
0

实际上,使用列表和字典可能是一个不错的选择。您正在谈论的“指针事物”默认情况下发生在 .NET 中的对象(任何类和/或结构)中。.NET 中的所有对象都是通过引用传递的。

所以,如果你使用:

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

您的输出将是“真实的”。

祝你好运!

于 2012-09-08T01:27:46.653 回答
0

我不建议使用 OrderedDictionary,因为它是一个非通用容器。

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

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

如果你想移植这个,这里有一些提示:

  1. 删除任何不可用的接口
  2. 移除序列化相关代码
  3. 将 ArrayList 替换为List<object>
  4. 将哈希表替换为Dictionary<object, object>
于 2012-09-08T10:20:05.650 回答