在.NET通用词典更改订单 [英] Changing the Order in a .NET Generic Dictionary

查看:172
本文介绍了在.NET通用词典更改订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从一个普通的字典继承了如下一类:

  MyClass类:System.Collections.Generic.Dictionary< ; INT,对象> 



我在一个特定的顺序添加值的列表,但我现在希望改变这种状况订购。有什么办法(无需拆卸和重新添加),我可以有效地重新索引值;因此在指数1更改对象现在处于例如指数10?例如,这不起作用:

  MyClass的[1]的.index = 10; 


解决方案

本身字典没有索引顺序。可考虑从 KeyedCollection 类继承来代替。它是一个字典和一个普通的列表的合并,它的设计使用的项目,如钥匙的一员,并有一个索引顺序


I have a class that inherits from a generic dictionary as follows:

Class myClass : System.Collections.Generic.Dictionary<int, Object>

I have added a list of values to this in a particular order, but I now wish to change that order. Is there any way (without removing and re-adding) that I could effectively re-index the values; so change the object at index 1 to now be at index 10 for example? For example, this doesn't work:

myClass[1].Index = 10;

解决方案

Dictionaries by themselves don't have an index order. Consider inheriting from the KeyedCollection class instead. It's a merge of a dictionary and an ordinary list, and it's designed to use a member of your items as the key, and have an index order.

这篇关于在.NET通用词典更改订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆