问题标签 [ienumerator]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
7 回答
24857 浏览

c# - 从数组中获取通用枚举器

在 C# 中,如何从给定数组中获取泛型枚举数?

在下面的代码中,MyArray是一个MyType对象数组。我想以MyIEnumerator显示的方式获得,但似乎我获得了一个空的枚举器(尽管我已经确认MyArray.Length > 0)。

0 投票
6 回答
24075 浏览

c# - IEnumerator 是否有“HasNext”方法?

在 JavaIterator中,我使用该hasNext方法来确定迭代是否有更多元素(不消耗元素)——因此,hasNext就像一个 " Peek" 方法。

我的问题: C# 的泛型s是否有类似 " hasNext" 或 " " 的方法?PeekIEnumerator

0 投票
1 回答
2109 浏览

c# - 如何从匿名类型 IEnumerable 中选择项目子集?

我有以下代码。

我不得不走匿名类型路线,因为 CodeDescription 不是 benefitCode 的属性,并且客户希望它以这种方式出现在 dropDrownList 中。无论如何,我的问题是如何从该列表中选择项目的子集?我需要根据 BenInterest 属性选择项目。

所以这会返回 IEnumerable,所以我试图走这条路,这就是我卡住的地方。我的意图是构建一个新的 IEnumerable 列表并为其设置一个下拉数据源。

那么如何创建一个仅包含所需项目的相同匿名类型的新 Enumerable。

谢谢你的帮助。干杯,~ck

0 投票
2 回答
870 浏览

c# - 从接口类调用 IEnumerator

我是枚举集合的新手,如果这个问题听起来很傻,请原谅。

我有一堂课

如您所见,我在上面实现了两个接口:一个是 IPersonStuff,另一个是 IEnumerable。

Person 类是一个简单的 getter/setter,每种类型都是字符串,例如姓名、地址,但 dateofbirth 是 DateTime。

在我的 IPersonStuff 中,我有这个:

一旦工作,我想以下列方式调用它(从任何类):

但是,我收到以下错误:

  • 'IEnumerable<...>.GetEnumerator':显式接口声明只能在类或结构中声明

基本上我想知道如何通过 IPersonStuff 接口调用我在 PersonStuff 类中的 IEnumerator IEnumerable.GetEnumerator() 。

0 投票
4 回答
2289 浏览

c# - .Net IList 的 IEnumerator 是否提供列表的顺序?

我可以假设IEnumerator我从一个IList(通过从接口调用GetEnumerator方法IEnumerable)得到的项目会按列表的顺序给我吗?

0 投票
3 回答
927 浏览

c# - C#:在哪里实现自定义 IEnumerator

假设我有一个实现IEnumerable<T>. 它目前在方法中使用yield关键字。GetEnumerator()但是现在我需要做更多的事情,例如我想自己清理一下。为此,除非我忽略了任何东西,否则我需要实现IEnumerator<T>接口。但是你说我应该在哪里做呢?

类本身是否应该实现它并GetEnumerator()返回this?还是将其隐藏在私人课程中会更好?还是应该只是一个完全不同的班级?对此有哪些常见做法?

0 投票
1 回答
14195 浏览

c# - 将 CollectionBase 转换为可用于 Linq 的列表或数据类型

我正在使用 Aspose 单元格来操作 Excel 电子表格。API 中的一种类型是电子表格中的图片集合,它派生自 CollectionBase:

看到这个链接: http ://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/aspose.cells.pictures.html

我想将此类型转换为允许我使用 Linq 表达式的类型

有哪些选择?

我想我可以遍历它并手动将其添加到 anew List<Picture> 但是有更好的方法吗?

我已阅读此问题 将 IEnumerable<T> 添加到从 CollectionBase 派生的类

但我显然无法控制实现 CollectionBace 的类,因为它是第三方产品

0 投票
3 回答
316 浏览

.net - Is it possible to get an IEnumerator from a T[]?

Let's say I want to create a collection class that is thread-safe by default.

Internally, the class has a protected List<T> property called Values.

For starters, it makes sense to have the class implement ICollection<T>. Some of this interface's members are quite easy to implement; for example, Count returns this.Values.Count.

But implementing ICollection<T> requires me to implement IEnumerable<T> as well as IEnumerable (non-generic), which is a bit tricky for a thread-safe collection.

Of course, I could always throw a NotSupportedException on IEnumerable<T>.GetEnumerator and IEnumerable.GetEnumerator, but that feels like a cop-out to me.

I already have a thread-safe getValues function which locks on Values and returns a copy in the form of a T[] array. So my idea was to implement GetEnumerator by returning this.getValues().GetEnumerator() so that the following code would actually be thread-safe:

Unfortunately this implementation only seems to work for IEnumerable.GetEnumerator, not the generic version (and so the above code throws an InvalidCastException).

One idea I had, which seemed to work, was to cast the T[] return value from getValues to an IEnumerable<T> before calling GetEnumerator on it. An alternative would be to change getValues to return an IEnumerable<T> in the first place, but then for the non-generic IEnumerable.GetEnumerator simply cast the return value from getValues to a non-generic IEnumerable. But I can't really decide if these approaches feel sloppy or perfectly acceptable.

In any case, does anyone have a better idea of how to go about doing this? I have heard of the .Synchronized methods, but they seem to only be available for non-generic collections in the System.Collections namespace. Maybe there is a generic variant of this that already exists in .NET that I simply don't know about?

0 投票
6 回答
1968 浏览

c# - 为什么.NET 没有双向枚举器?

在 SO 上已多次询问如何实现双向枚举器(此处此处)。我的问题不是如何(在大多数情况下这是微不足道的),而是为什么.NET 平台中不存在这种类型。

显然,有许多集合类型无法实现这一点,因为它们MoveNext()具有破坏性或改变底层集合的状态。但相反,许多类型可以简单地实现这一点(List, IList, LinkedList, array)。

为什么不存在这种类型?

0 投票
9 回答
30073 浏览

c# - 返回一个空的 IEnumerator

我有一个接口,除其他外,它实现了“公共 IEnumerator GetEnumerator()”方法,因此我可以在 foreach 语句中使用该接口。

我在几个类中实现了这个接口,在其中一个类中,我想返回一个空的 IEnumerator。现在我通过以下方式执行此操作:

但是我认为这是一个丑陋的 hack,我不禁认为有更好的方法来返回一个空的 IEnumerator。在那儿?