Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows Phone 7-订购词典/备选方案_C#_.net_Windows Phone 7_Collections - Fatal编程技术网

C# Windows Phone 7-订购词典/备选方案

C# Windows Phone 7-订购词典/备选方案,c#,.net,windows-phone-7,collections,C#,.net,Windows Phone 7,Collections,我对C#完全是新手,所以我要对我自己的OrderedDictionary版本进行一次可怕的尝试,除非有人能提出替代方案 我需要能够通过数组索引访问我的元素,保留它们添加的顺序,并且我还将经常使用它们的键更新单个元素 是否有允许在手机上使用此功能的收藏 如果我保留一个列表和一本字典,它们是否都指向同一个项目,或者我是否需要做一些指针操作 Item i = new Item(); list.Add(i); dict.Add("key", i); 实际上,使用列表和字典可能是一个不错的选择。您所说

我对C#完全是新手,所以我要对我自己的OrderedDictionary版本进行一次可怕的尝试,除非有人能提出替代方案

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

  • 是否有允许在手机上使用此功能的收藏

  • 如果我保留一个列表和一本字典,它们是否都指向同一个项目,或者我是否需要做一些指针操作

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

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

    因此,如果您使用:

    Item i = new Item();
    list.Add(i);
    dict.Add("key",i);
    Console.WriteLine(list.Last() == dict["key"]);
    
    您的输出将为“真”


    祝你好运

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

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

    如果要移植此端口,以下是一些提示:

  • 删除任何不可用的接口
  • 删除与序列化相关的代码
  • 将ArrayList替换为
    列表
  • 将哈希表替换为
    字典
  • 以下是我的实现(来自):

    公共类OrderedDictionary:IEnumerable { 私家词典; 私有列表m_List=新列表(); 私有对象m_syncRoot=新对象(); 公共秩序词典 { m_dictionary=新字典(); } 公共命令字典(IEqualityComparer comparer comparer) { m_dictionary=新字典(比较器); } 公共无效添加(TKey键,TValue值) { 锁定(m_syncRoot) { m_dictionary.Add(键、值); m_列表。添加(值); } } 公共TValue此[int索引] { 获取{return m_list[index];} } 公共TValue此[TKey] { 获取{return m_dictionary[key];} } 公共整数计数 { 获取{return m_dictionary.Count;} } public Dictionary.KeyCollection密钥 { 获取{return m_dictionary.Keys;} } public Dictionary.ValueCollection值 { 获取{返回m_dictionary.Values;} } 公共空间清除() { 锁定(m_syncRoot) { m_dictionary.Clear(); m_list.Clear(); } } 公共bool ContainsKey(TKey) { 返回m_dictionary.ContainsKey(键); } 公共布尔值包含值(TValue值) { 返回m_dictionary.ContainsValue(值); } public void Insert(int索引、TKey键、TValue值) { 锁定(m_syncRoot) { m_列表。插入(索引、值); m_dictionary.Add(键、值); } } 公共无效删除(TKey) { 锁定(m_syncRoot) { if(集装箱箱(钥匙)) { var existing=m_dictionary[key]; m_列表。删除(现有); m_字典。删除(键); } } } 公共IEnumerator GetEnumerator() { 返回m_dictionary.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { 返回GetEnumerator(); } }
    这看起来不错。不幸的是,它没有提供快速获取特定项目索引的方法。此外,它不支持
    RemoveAt()
    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();
        }
    }