c# - IEnumerable<T> 是隐含的顺序相关性还是应该是明确的?

标签 c# .net linq ienumerable

对我来说,IEnumerable<T>在 C#(好吧,.NET)中表示可以迭代的任意数据集。它可以由任何东西支持,比如 SELECT 的结果。查询、数组的内容、用户在控制台中键入的字符或 Pi 的数字。数据不能被索引引用,不一定是有限的或无限的,也不能被修改。当稍后以相同方式调用时,它甚至可能是一组不同的数据,例如 IEnumerable<double>。的随机数。它只是像 foreach 一样提供给消费者的数据位。循环。

现在考虑处理数据集的其他事物中的一个概念:SQL。在 SQL 中,除非明确指定,否则不保证行的顺序并且不相关。例如,如果您执行 SELECT * FROM stack_overflow_posts LIMIT 1 ,数据库没有暗示您返回的行实际上是插入的第一行,也不是最旧的行。您需要使用 ORDER BY posted_date_time 明确排序结果,例如。

同样的概念是否适用于 .NET 中的枚举 IEnumerable<T>是利用IEnumerable<T>结果总是按特定顺序产生的暗示?在我之前给出的例子中,我会说是的,顺序是隐含的,因为如果它们以不同的顺序列举,结果将毫无意义;如果您得到用户在控制台中键入的字符的顺序与实际击键的顺序不同,那么读取它们有什么意义呢?显然 LINQ 有 OrderBy()根据需要对结果进行排序,但这是显式排序,而不是隐式排序。

实现接口(interface)的类有效地 promise 遵循特定的模式,而不仅仅是实现接口(interface)定义的方法。是否IEnumerable<T>暗示其数据将按相关顺序生成,或者是否由枚举的消费者明确排序(如果他们希望这样做)?如果我有一个方法以未定义的顺序生成项目——或者更确切地说,一个与消费者无关并且随时可能发生变化的顺序——我应该使用 IEnumerable<T> 以外的东西吗? ?

最佳答案

Is the use of IEnumerable<T> an implication that results will always be yielded in a particular order?

没有。 IEnumerable只是保证对象可以被迭代。事实List<T> ,例如,始终根据索引按升序输出项目是 List<T> 的一个实现细节具体来说。

A class implementing an interface is effectively promising to follow a particular pattern, not just implement methods as defined by the interface. Does IEnumerable<T> imply its data will be yielded in a relevant order, or is it up to consumers of the enumeration to explicitly order if they wish to do so?

不,实现IEnumerable并不意味着任何顺序。使用 IEnumerable 的对象时, 如果您想保证您的数据每次都以相同的顺序输出,则必须明确提供顺序。

如果您想到实现 IEnumerable 的 CLR 集合类型,这很简单。假设您创建了一个返回 IEnumerable<string> 的方法.该方法可以返回 List<string> ,其实现 IEnumerable 确实有一定的顺序,但它可以很容易地返回 HashSet<string> ,订单没有意义。

If I have a method that yields items in an undefined order--or rather, an order that is not relevant to consumers and is subject to change at any point--should I use something other than IEnumerable<T>?

我会说 IEnumerable<T>非常适合您的需求。为了更加清楚,您可以记录您的方法并声明结果中的项目顺序未定义并且可能会因调用而异。

关于c# - IEnumerable<T> 是隐含的顺序相关性还是应该是明确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26306936/

相关文章:

c# - "ASP.NET 4.5 Async Pipeline"是什么意思?

.net - 当 CLR 在 .net 中调用垃圾收集器时?

c# - 林克 : select value in a datatable column

linq查询匿名类型无法转换为POCO对象

javascript - 通过 JavaScript fetch() 发布到 Razor 处理程序时值为空

c# - 从 C# 连接到 MemSQL

c# - 如何在服务器标签中使用强类型资源?

c# - 屏幕分辨率和滚动条

asp.net - 如何获取任何 asp :LinkButton to look like a button

c# - 使用 SUM() 将 SQL 语句转换为 Linq/Entity Framework