vb.net 允许相同密钥的通用集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14146613/
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-09-17 11:45:50  来源:igfitidea点击:

Generic Collection which allows same key

c#.netvb.netcollections

提问by Elmo

Collections like HashTableand Dictionarydon't allow to add a value with the same key but I want to store the same values with the same keys in a Collection<int,string>.

集合喜欢HashTable并且Dictionary不允许添加具有相同键的值,但我想将具有相同键的相同值存储在Collection<int,string>.

Is there a built-in collection which lets me do this?

是否有内置的集合可以让我这样做?

回答by Reed Copsey

You can use a List<T>containing a custom class, or even a List<Tuple<int,string>>.

您可以使用List<T>包含自定义类的 ,甚至可以使用List<Tuple<int,string>>.

List<Tuple<int,string>> values = new List<Tuple<int,string>>();

values.Add(Tuple.Create(23, "Foo"));
values.Add(Tuple.Create(23, "Bar"));

Alternatively, you can make a Dictionary<int, List<string>>(or some other collection of string), and populate the values in that way.

或者,您可以创建一个Dictionary<int, List<string>>(或其他一些字符串集合),并以这种方式填充值。

Dictionary<int, List<string>> dict = new Dictionary<int, List<string>>();
dict.Add(23, new List<string> { "Foo", "Bar" });

This has the advantage of still providing fast lookups by key, while allowing multiple values per key. However, it's a bit trickier to add values later. If using this, I'd encapsulate the adding of values in a method, ie:

这样做的优点是仍然可以通过键提供快速查找,同时允许每个键有多个值。但是,稍后添加值有点棘手。如果使用它,我会将值的添加封装在一个方法中,即:

void Add(int key, string value)
{
    List<string> values;
    if (!dict.TryGetValue(key, out values))
    {
        values = new List<string>();
        dict[key] = values;
    }

    values.Add(value);
}

回答by Dave Zych

Use a Listwith a custom Class.

将 aList与自定义Class.

public class MyClass
{
    public int MyInt { get; set; }
    public string MyString { get; set; }
}

List<MyClass> myList = new List<MyClass>();
myList.Add(new MyClass { MyInt = 1, MyString = "string" });

回答by Yusubov

In short:The easiest way to go would be a generic List<T>collection while skiping the ArrayListclass. Because, there are some performance considerationsthat you need to take into account.

简而言之:最简单的方法是List<T>跳过ArrayList课程时使用泛型集合。因为,您需要考虑一些性能因素

In addition, you can also use List<KeyValuePair<string,int>>. This will store a list of KeyValuePair's that can be duplicate.

此外,您还可以使用List<KeyValuePair<string,int>>. 这将存储KeyValuePair可以重复的's列表。

In deciding whether to use the List<T> or ArrayListclass, both of which have similar functionality, remember that the List<T>class performs betterin most cases and is type safe. If a reference type is used for type T of the List<T>class, the behavior of the two classes is identical. However, if a value type is used for type T, you need to consider implementation and boxing issues.

在决定是否使用List<T> or ArrayList具有相似功能的List<T>类时,请记住该类在大多数情况下性能更好,并且是类型安全的。如果类的类型 T 使用引用类型List<T>,则两个类的行为是相同的。但是,如果值类型用于类型 T,则需要考虑实现和装箱问题。

As reference:you may use the following MSDN article - List Class.

作为参考:您可以使用以下 MSDN 文章 - List Class