在 C# 中存储重复的键值对

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1927589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:55:43  来源:igfitidea点击:

Storing duplicate key value pairs in C#

c#collections

提问by Royson

I have a data like in (string , int) pair. How to store this data in collection object. Both values can be duplicate. Which collection object should i use??

我有一个类似 (string , int) 对的数据。如何将此数据存储在集合对象中。两个值都可以重复。我应该使用哪个集合对象?

EDIT: How can i access elements separately..??

编辑:我如何分别访问元素..??

采纳答案by Oded

You can use List<KeyValuePair<string,int>>.

您可以使用List<KeyValuePair<string,int>>.

This will store a list of KeyValuePair's that can be duplicate.

这将存储KeyValuePair可以重复的's列表。

回答by thecoop

You can use List<KeyValuePair<string, int>>if you want to add & remove items, or KeyValuePair<string, int>[]if the number of items is known

List<KeyValuePair<string, int>>如果要添加和删除项目,或者KeyValuePair<string, int>[]如果项目数量已知,您可以使用

回答by Brian Rasmussen

If you want to avoid repeating the key for multiple values you can use Dictionary<string, List<int>>.

如果您想避免为多个值重复键,您可以使用Dictionary<string, List<int>>.