.net KeyValuePair 列表

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

List of KeyValuePair

.net

提问by serhio

Why does .NET not provide a class for a List<KeyValuePair<T, U>>?

为什么 .NET 不为 a 提供类List<KeyValuePair<T, U>>

I think there are a lot of situations when you need to keep an array of pairs. For example,

我认为有很多情况需要保留一组对。例如,

1; 2      "a"; "array"
5; 8      "b"; "browser"
1; 9      "f"; "Firefox"
8; 10     "f"; "frequency"

à la:

à la:

Pairs<int, int> myPairs;

myPairs.Add(10, 8);
myPairs.Add(5, 8);
myPairs.Add(10, 5);
myPairs.Add(1, 4);

myPairs[0].Value1 = 5;
myPairs[5].Value2 = 8;

回答by Jon Skeet

This seems completely unnecessary to me - and just because you have a pair of values doesn't mean it's necessarily a key/value relation either.

这对我来说似乎完全没有必要——仅仅因为你有一对值并不意味着它也一定是键/值关系。

.NET 4 introduced the Tuplefamily of types... unless this really wasa key/value pair, I'd use List<Tuple<T1, T2>>instead - and I see no reason for a single type to exist in order to encapsulate that construct.

.NET 4 引入了Tuple类型系列……除非这确实一个键/值对,否则我会使用它List<Tuple<T1, T2>>- 我认为没有理由为了封装该构造而存在单一类型。

EDIT: To put this into context, here's how "difficult" it is to use Tuplehere, converting your sample code:

编辑:为了将其置于上下文中,这是在Tuple这里使用的“困难” ,转换您的示例代码:

var myPairs = new List<Tuple<int, int>> {
    Tuple.Create(10, 8),
    Tuple.Create(5, 8),
    Tuple.Create(10, 5),
    Tuple.Create(1, 4),
};

Now tuples are immutable, so you wouldn't be able to set the values directly as per the last part of your code... but the beauty of composingthe two concepts of "list" and "pair of values" is that you could easily write your own MutableTupletype and use exactly the same code... whereas if you had one collection class which was hard-wired to Tupleor some other type, you wouldn't have that flexibility.

现在元组是不可变的,因此您将无法按照代码的最后一部分直接设置值……但是组合“列表”和“值对”这两个概念的美妙之处在于您可以轻松编写您自己的MutableTuple类型并使用完全相同的代码......而如果您有一个硬连接到Tuple或其他类型的集合类,则不会有这种灵活性。

回答by Dan Tao

Why stop there? Why not a list of triplets? Or quadruplets?

为什么要停在那里?为什么不是三胞胎列表?还是四胞胎?

The fact is that this is verytrivial to write yourself. Write a class that derives from List<KeyValuePair<TKey, TValue>>, add an Addmethod that takes 2 parameters.

事实是,这对自己编写非常微不足道。编写一个派生自 的类List<KeyValuePair<TKey, TValue>>,添加一个带有Add2 个参数的方法。

public class KeyValueList<TKey, TValue> : List<KeyValuePair<TKey, TValue>>
{
    public void Add(TKey key, TValue value)
    {
        Add(new KeyValuePair<TKey, TValue>(key, value));
    }
}

Boom, you're done.

砰,你完成了。



Further comments: Note that KeyValuePair<TKey, TValue>is an immutable type (as is Tuple<T1, T2>in .NET 4.0); so if you'd want to be able to do something like this:

进一步评论:请注意,这KeyValuePair<TKey, TValue>是一个不可变类型(如Tuple<T1, T2>.NET 4.0 中一样);所以如果你想能够做这样的事情:

list[0].Value = 5;

...then you'd need a mutable type, something like:

...那么你需要一个可变类型,比如:

public sealed class Pair<T1, T2>
{
    public T1 X { get; set; }
    public T2 Y { get; set; }
}

public class PairList<T1, T2> : List<Pair<T1, T2>>
{ /* ...same as KeyValueList<T1, T2>, basically... */ }

Also note that this would enable nice initialization syntax:

另请注意,这将启用漂亮的初始化语法:

var list = new PairList<int, string>
{
    { 1, "dan" },
    { 2, "serhio" }
};

But again, the point here is that this is very easy and very trivial and thus not really deserving of its own type in the framework. Look at it this way: can you name any type in the BCL that is essentially just anothertype but with one single convenience method added?

同样,这里的重点是这非常容易且非常琐碎,因此在框架中并不真正值得拥有自己的类型。这样看:你能在 BCL 中命名任何类型,它本质上只是另一种类型,但添加了一个方便的方法吗?



Closing comments: You have posed these questions:

结束语:您提出了以下问题:

  • Why introduce a List<T>type when you could just use a T[]array?
  • Why introduce a Dictionary<TKey, TValue>type when you could just use a List<KeyValuePair<TKey, TValue>>?
  • List<T>当您可以只使用T[]数组时,为什么要引入类型?
  • Dictionary<TKey, TValue>当您可以只使用 a 时,为什么要引入类型List<KeyValuePair<TKey, TValue>>

These are bizarre questions, honestly. Why introduce OOP at all when you can just do everything with procedural code and global variables? Why introduce higher-level programming languages like C#, C++, or even C when you could just write everything in Assembly?

老实说,这些都是奇怪的问题。当您可以使用过程代码和全局变量完成所有操作时,为什么还要引入 OOP?当您可以在汇编中编写所有内容时,为什么还要引入更高级的编程语言,如 C#、C++ 甚至 C?

The List<T>class provides a useful encapsulationof all the functionality that goes into accessing elements in an array, maintaining the number of items, resizing the array as necessary, etc. There is a big differencebetween what you can do with a List<T>and what you can do with just a T[].

List<T>类提供了一个有用的封装的所有功能,进入访问数组中的元素,保持项目的数量,调整数组作为必要的,等有一个很大的区别之间,你可以用做什么List<T>,你可以做什么只需一个T[].

The Dictionary<TKey, TValue>also encapsulates the functionality of maintaining a collection of values associated with unique keys. It also provides killer O(1) lookup time on those keys. There is a huge differencebetween a Dictionary<TKey, TValue>and a List<KeyValuePair<TKey, TValue>>.

Dictionary<TKey, TValue>还封装保持与独特的键关联的值的集合的功能。它还提供了对这些键的杀手级 O(1) 查找时间。a和 a之间存在巨大差异Dictionary<TKey, TValue>List<KeyValuePair<TKey, TValue>>

The difference between a List<KeyValuePair<TKey, TValue>>and your proposed KeyValueList<TKey, TValue>(as I've called it above) is practically nil. Hardly anything new is encapsulated. It just isa List<T>. Honestly the benefit is, to me, hardly greater than adding an Int32Listtype that just wraps a List<int>.

aList<KeyValuePair<TKey, TValue>>和你提议的KeyValueList<TKey, TValue>(正如我上面所说的)之间的区别几乎为零。几乎没有任何新内容被封装。它只是一个List<T>。老实说,对我来说,好处几乎不比添加一个Int32List只包装List<int>.

There's just no reason to add such a type to the BCL.

没有理由将这种类型添加到 BCL。

回答by thecoop

What's stopping you from using a List<KeyValuePair<T, V>>directly? And, if you know Tand V, you can alias it at the top of your source file:

是什么阻止你List<KeyValuePair<T, V>>直接使用 a ?而且,如果您知道TV,您可以在源文件的顶部为它设置别名:

using KeyValueList = System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<MyNS.MyKeyType, MyNS.MyValueType>>;

// in your code...
KeyValueList list = new KeyValueList();
list.Add(new KeyValuePair<MyKeyType, MyValueType>(..., ...));

回答by Andrew Hare

You may be looking for Dictionary<TKey,TValue>:

您可能正在寻找Dictionary<TKey,TValue>

Represents a collection of keys and values.

表示键和值的集合。

Dictionary<TKey,TValue>is a specialized collection that is optimized for key/value pairs. While you are free to create a List<KeyValuePair<TKey,TValue>>, you are short-changing yourself as you will not have optimized access to your keys are values.

Dictionary<TKey,TValue>是一个专门为键/值对优化的集合。虽然您可以自由地创建一个List<KeyValuePair<TKey,TValue>>,但您正在改变自己,因为您将无法优化对键值的访问。

回答by David M?rtensson

You could use System.Collections.Specialized.NameValueCollection, that's a KeyValue store ;)

您可以使用System.Collections.Specialized.NameValueCollection,这是一个 KeyValue 存储;)

It's pure (string, string)mapping, but then most key value situations would work with that.

这是纯粹的(string, string)映射,但大多数关键值情况都可以使用。

Or make a subclass of System.Collections.Specialized.NameObjectCollectionBasefor a special solution for your object type.

或者System.Collections.Specialized.NameObjectCollectionBase为您的对象类型创建一个特殊解决方案的子类。