C#元组与列表注意事项 [英] C# Tuple versus List Considerations

查看:48
本文介绍了C#元组与列表注意事项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

元组和列表的不同属性;

The different attributes of Tuples and Lists;

  • 组合是异类的,列表是同质的,
  • 列表可变时,语音提示是不变的

通常指示使用一种类型而不是另一种类型.但是,在其他情况下,任何一种数据类型都可能同样适用.因此,元组与列表的内存和/或性能含义对我们的决策有什么指导?
谢谢,

often dictate the use of one type over the other. In other scenarios, however, either data type could be equally appropriate. As such , what are the memory and/or performance implications of Tuples versus Lists that might also guide our decision?
Thanks,

推荐答案

好吧,除了您提到的内容之外,元组最多只能包含八个项目,这还有很大的不同.(好的,您可以通过将最后一个元组参数类型设置为另一个元组来从技术上来创建任意大的元组,但是我不禁感到您实际上必须有点发疯.)

Well, in addition to what you've mentioned there's the rather significant difference that a tuple can only contain up to eight items. (OK, you can technically make arbitrarily large tuples by making the last tuple argument type another tuple, but I can't help but feel that you'd have to be slightly insane to actually do that.)

与Python等语言中的元组不同,C#中的元组实际上不能用作通用数据结构.C#中元组最常见的用例之一是从一个函数返回多个值,或者将多个值传递给由于某些原因只能取一个的函数(例如,将 e.Argument 传递给 BackgroundWorker ),或在任何其他情况下都不会麻烦您创建自定义类并且不能使用匿名类型的情况.

Unlike tuples in a language like Python, tuples in C# can't really be used as a general purpose data structure. One of the most common use cases for a tuple in C# is for returning multiple values from a function, or passing multiple values to functions that for some reasons can only take one (e.g. when passing e.Argument to a BackgroundWorker), or any other situation where you can't be bothered to make a custom class, and you can't use an anonymous type.

由于您需要确切地知道在编译时它们将包含多少个项目(以及什么类型的项目),因此元组的使用实际上受到严格限制.另一方面,列表是用于同类数据的通用存储,您不一定知道要有多少个项目.我很乐意看到一段代码的示例,正​​如您所说的那样,任何一种数据类型都可以同样适用".

Since you need to know exactly how many items (and what types of items) they will contain at compile time, tuples are really of severely limited use. Lists, on the other hand, are for general purpose storage of homogeneous data, where you don't necessarily know how many items you're going to have. I'd love to see an example of a piece of code where, as you put it, "either data type could be equally appropriate".

此外,由于元组和列表解决了完全不同的问题,因此比较内存/性能的含义可能是相当有限的兴趣.但是就其价值而言,元组是作为类而不是结构实现的,因此它们像列表一样存储在堆中,并且当您在函数之间传递它们时,它们不会被复制,这与值类型不同.但是,它们确实实现了 IStructuralEquatable IStructuralComparable 接口,并且实现了它们的 Equals 方法,以便其返回true: new Tuple<int>(1).Equals(new Tuple< int>(1))(同时, new List< int>(){)} .Equals(new List< int>(){)})为假).

Furthermore, since tuples and lists solve completely different problems, it's probably of fairly limited interest to compare the memory/performance implications. But for what it's worth, tuples are implemented as classes, not as structs, so they're stored on the heap just like lists, and they aren't copied when you pass them between functions, unlike value types. They do however implement the IStructuralEquatable and IStructuralComparable interfaces, and their Equals method is implemented such that this will return true: new Tuple<int>(1).Equals(new Tuple<int>(1)) (meanwhile, new List<int>() { 1 }.Equals(new List<int>() { 1 }) is false).

这篇关于C#元组与列表注意事项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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