Tag: 枚举器

我什么时候应该单独实现IEnumerator ?

在集合的框架类中,我经常看到IEnumerator分别实现为内部类,并且在GetEnumerator方法中返回它的实例。 现在假设我正在编写自己的集合类,它将内置集合,如List或T[]充当内部持有者,如下所示: public class SpecialCollection : IEnumerable { List list; public SpecialCollection() { } public IEnumerator GetEnumerator() { return list.GetEnumerator(); //or return list.Where(x => some logic).GetEnumerator(); //or directly rely on yield keyword yield return x; //etc } } 我应该编写自己的枚举器类,还是可以返回List类的枚举器? 我是否应该编写自己的枚举类? 我也有一个相关的问题。 如果它不是那么重要或没有太大的区别,为什么BCL中的每个集合类都编写自己的IEnumerator ? 例如, List类有类似的东西 T[] items; public IEnumerator GetEnumerator() { return new List.Enumerator(items); }

集合已修改,枚举操作可能无法执行

我有multithreading应用程序,我得到这个错误 ************** Exception Text ************** System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.List`1.Enumerator.MoveNextRare() at System.Collections.Generic.List`1.Enumerator.MoveNext() … 我的collections可能有问题,因为在一个线程上我读取了我的collections,在另一个线程上我修改了collections。 public readonly ObservableCollectionThreadSafe Markers = new ObservableCollectionThreadSafe(); public void problem() { foreach (GMapMarker m in Markers) { … } } 我试图用这段代码锁定集合,但不起作用。 public void problem() { lock(Markers) { foreach (GMapMarker m in Markers) […]

将非generics集合转换为generics集合的最佳方法

将非generics集合转换为generics集合的最佳方法是什么? LINQ有办法吗? 我有以下代码。 public class NonGenericCollection:CollectionBase { public void Add(TestClass a) { List.Add(a); } } public class ConvertTest { public static List ConvertToGenericClass( NonGenericCollection collection) { // Ask for help here. } } 谢谢!

为什么C#中没有ReverseEnumerator?

有没有人知道是否有特定的理由或设计决定不在C#中包含反向调查员? 如果有一个等效的C ++ reverse_iterator就好了,就像Enumerator相当于C ++ iterator 。 可以反向迭代的集合只会实现像IReverseEnumerable这样的东西,可以做类似的事情: List.ReverseEnumerator ritr = collection.GetReverseEnumerator(); while(rtir.MoveNext()) { // do stuff } 这样,您就可以以相同的方式迭代Lists和LinkedLists,而不是使用索引器为另一个链接和之前的链接,从而实现更好的抽象

不使用,foreach或手动调用Dispose()时的枚举器处理

我正在使用yield return迭代SqlDataReader的记录: IEnumerable GetReadings() { using (var connection = new SqlConnection(_connectionString)) { using (var command = new SqlCommand(_query, connection)) { connection.Open(); using (var reader = command.ExecuteReader()) { while (reader.Read()) { yield return new Reading { End = reader.GetDateTime(0), Value = reader.GetDouble(1) }; } } connection.Close(); } } } 然后我使用这个被接受的答案的改编版本来“拉”许多迭代器: var enumerators = data.Select(d => new […]

实现IEnumerable 和IEnumerable.GetEnumerator()不能公开,为什么?

要实现接口成员,实现类的相应成员必须是公共的。 source: 接口(C#编程指南) 我知道它是私有的,但我想知道它为什么不公开?

从枚举中获取下一个N个元素

上下文:C#3.0,.Net 3.5 假设我有一个生成随机数的方法(永远): private static IEnumerable RandomNumberGenerator() { while (true) yield return GenerateRandomNumber(0, 100); } 我需要将这些数字分组为10组,所以我想要像: foreach (IEnumerable group in RandomNumberGenerator().Slice(10)) { Assert.That(group.Count() == 10); } 我已经定义了Slice方法,但我觉得应该已经定义了一个。 这是我的Slice方法,仅供参考: private static IEnumerable Slice(IEnumerable enumerable, int size) { var result = new List(size); foreach (var item in enumerable) { result.Add(item); if (result.Count == size) { yield return […]