[英]Sorted list items
Is it possible to add items to sorted list with the same value, because when I am trying to do so it shows me an error: 是否可以将项目添加到具有相同值的排序列表中,因为当我尝试这样做时它会显示错误:
"An entry with the same key already exists." “已存在具有相同密钥的条目。”
If it's possible, then how? 如果有可能,那怎么样?
It is not possible* to add duplicate keys as stated by other users. 如其他用户所述,添加重复密钥是不可能的。
In c# you might be able to use the Lookup class instead, which allows multiple values to be stored with the same key. 在c#中,您可以使用Lookup类,它允许使用相同的键存储多个值。
See: http://msdn.microsoft.com/en-us/library/bb460184.aspx 请参阅: http : //msdn.microsoft.com/en-us/library/bb460184.aspx
* It is possible, see comments, but only by defining a comparitor that never returns equality for equal items, which IMO is a really really bad idea. *有可能,请参阅注释,但只能通过定义一个永远不会返回相等项目的相等性的比较器,IMO真的是一个非常糟糕的主意。
No, it's not possible. 不,这是不可能的。 The keys must be unique. 密钥必须是唯一的。
Define a class that implements IComparer
. 定义一个实现IComparer
的类。 When you instantiate the SortedList
, you pass in an instance of your class. 实例化SortedList
,将传入类的实例。 Check out Knasterbax's answer . 查看Knasterbax的答案 。
Key should be unique. 密钥应该是唯一的。 See this in MSDN 在MSDN中看到这一点
ArgumentException - An element with the specified key already exists in the SortedList object.
http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.add.aspx http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.add.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.