.NET:测试 IEnumerable 是否无限或禁止无限集合的简单方法

发布于 2025-01-17 22:15:40 字数 733 浏览 0 评论 0 原文

我在处理无限的ienumerable ,但我没有t看到一个很好的答案,以测试枚举是否是无限的。

我的最终目标是为.NET中的所有收集类型编写一个函数,但要安全地穿越整个集合,而不会因某些无限收藏而出现长时间等待时间的风险。

OPT1:我认为我可能能够分辨出枚举是否是无限的,并且抛出了例外。但是,我认为这将是.NET聚合LINQ表达式(例如Max,Count)的方法。

Opt2:是否有其他一些抽象支持大多数所有枚举类型,但是不允许无限元素? iCollection 看起来很有希望,支持列表和数组,但不排队或许多自定义Ienumerables。

我也很难确定偶像是否可以无限。

建议?

I saw the post on ways to handle an infinite IEnumerable, but I didn't see a good answer for testing if an enumerable is infinite.

My end goal is to write a function for all collection types in .NET, but to safely traverse the whole collection without the risk of long wait times due to some infinite collection.

OPT1: I thought I might be able tell if an enumerable is infinite and throw an exception. However, I figure this would be the approach for .NET aggregate LINQ expressions (e.g. Max, Count) if it were possible.

  • EDIT: As suspected, another question was suggested that directly declares this option impossible

OPT2: Is there some other abstraction that supports most all enumerable types, but disallows infinite elements? ICollection looks promising, supports lists and arrays, but not Queues or many custom IEnumerables.

I'm also having a hard time determining if ICollection can be infinite or not.

Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2025-01-24 22:15:40

您实际上是遇到停止问题。没有通用算法可以用于确定图灵完整计算是否会完成。

您可以拥有 iEnumerable< int ,该列举了π中的所有数字。无法确定该功能是否一直在继续进行(根据数学,或者应像数学一样),或者仅估计π到十亿个小数。

是否还有其他一些抽象来支持大多数枚举类型,但是不允许无限元素?

我通常使用 IREADONLYCOLLECTION 仅出于这个目的。没什么,不过带有额外的 int 具有 maxValue

也就是说,恶意实施者可以通过仅说谎并设置 count to,例如 0 ,,可以轻率地实现 ireadonLyCollection< t> 作为无限序列因此没有保证。但是,由于任何任何 c#方法都可以假设地阻止,因此我认为这是该课程的标准。

我首先意识到 ireadonLyCollection代码>在2013年,此后不久就开始使用它。有时使用它仍然有些尴尬,因为许多其他API返回 iEnumerable< t> 。您可以轻松地将它们转换为 IREADONLYCOLLECTION< t> ,都可以使用 toarray 有时仍然有点不方便。

尽管如此,我一直曾经由递延执行执行足够的时间我强烈偏爱 iReadonLyCollection< t>

You are literally running into the Halting Problem. There's no general-purpose algorithm that can be used to determine whether or not a Turing-complete computation will ever complete.

You could have an IEnumerable<int> that enumerates all the digits in π. There's no way of telling if that function just keeps on going (as it should, according to mathematics), or only approximates π to a billion decimals.

Is there some other abstraction that supports most all enumerable types, but disallows infinite elements?

I usually use IReadOnlyCollection<T> for just that purpose. It's nothing but IEnumerable<T> with an extra Count property. That property strongly suggests that the collection has a finite length - particularly because int has a MaxValue.

That said, a malicious implementer could trivially implement IReadOnlyCollection<T> as an infinite sequence by just lying and setting Count to, say, 0, so there are no guarantees. But since any C# method could hypothetically block forever, I consider that par for the course.

I first became aware of IReadOnlyCollection<T> in 2013 and started using it soon thereafter. Using it is still a bit awkward at times, since so many other APIs return IEnumerable<T>. You can easily convert them to IReadOnlyCollection<T> with either ToArray or ToList, but it's still a little inconvenient at times.

Still, I've been bitten by deferred execution enough times that I strongly favour IReadOnlyCollection<T>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文