草庐IT

enumerator

全部标签

c# - 为什么在不修改枚举集合时得到 "Collection was modified; enumeration operation may not execute"?

这个问题在这里已经有了答案:Howtoremoveelementsfromagenericlistwhileiteratingoverit?(28个答案)关闭9年前。我有两个字符串集合:CollectionA是系统中存储的对象的StringCollection属性,而CollectionB是运行时生成的List。如果存在任何差异,则需要更新CollectionA以匹配CollectionB。因此,我设计了一个我期望的简单LINQ方法来执行删除。varstrDifferences=CollectionA.Where(foo=>!CollectionB.Contains(foo));for

c# - 即使是 "IsNullOrEmpty"检查也会给出 "Possible multiple enumeration of IEnumerable"警告

有一个questiononSOabout"possiblemultipleenumerations"已经,但这个问题更具体。请考虑以下方法,它需要一个IEnumerable作为输入并对其每个元素执行给定的方法:publicstaticboolSomeMethod(IEnumerableenumerable){if(enumerable.IsNullOrEmpty()){//throwexception.}else{return(enumerable.All(SomeBooleanMethod));}}在上面的代码中,IsNullOrEmpty只是一个运行的扩展方法return(!Ref

c# - 即使是 "IsNullOrEmpty"检查也会给出 "Possible multiple enumeration of IEnumerable"警告

有一个questiononSOabout"possiblemultipleenumerations"已经,但这个问题更具体。请考虑以下方法,它需要一个IEnumerable作为输入并对其每个元素执行给定的方法:publicstaticboolSomeMethod(IEnumerableenumerable){if(enumerable.IsNullOrEmpty()){//throwexception.}else{return(enumerable.All(SomeBooleanMethod));}}在上面的代码中,IsNullOrEmpty只是一个运行的扩展方法return(!Ref

c# - 为什么 C# 数组对枚举使用引用类型,而 List<T> 使用可变结构?

根据我的阅读,出于性能原因,设计决定将某些集合的枚举器类型设为可变结构而不是引用类型。List.Enumerator是最著名的。我正在调查一些使用数组的旧代码,并且惊讶地发现C#数组返回类型SZGenericArrayEnumerator作为它们的通用枚举器类型,这是一个引用类型。我想知道是否有人知道为什么Array的通用迭代器被实现为引用类型,而许多其他对性能至关重要的集合却使用可变结构。 最佳答案 FromwhatI'veread,adesigndecisionwasmadeforcertainCollections'sEnum

c# - 为什么 C# 数组对枚举使用引用类型,而 List<T> 使用可变结构?

根据我的阅读,出于性能原因,设计决定将某些集合的枚举器类型设为可变结构而不是引用类型。List.Enumerator是最著名的。我正在调查一些使用数组的旧代码,并且惊讶地发现C#数组返回类型SZGenericArrayEnumerator作为它们的通用枚举器类型,这是一个引用类型。我想知道是否有人知道为什么Array的通用迭代器被实现为引用类型,而许多其他对性能至关重要的集合却使用可变结构。 最佳答案 FromwhatI'veread,adesigndecisionwasmadeforcertainCollections'sEnum

c# - C# yield 语句的实现算法

我很想自己弄明白,但我想知道将带有yield语句的函数转换为枚举器状态机的大致算法是什么?例如,C#如何转换:IEnumeratorstrings(IEnumerableargs){IEnumeratorenumerator2=getAnotherEnumerator();foreach(vararginarg){enumerator2.MoveNext();yieldreturnarg+enumerator.Current;}}进入这个:boolMoveNext(){switch(this.state){case0:this.state=-1;this.enumerator2=get

c# - C# yield 语句的实现算法

我很想自己弄明白,但我想知道将带有yield语句的函数转换为枚举器状态机的大致算法是什么?例如,C#如何转换:IEnumeratorstrings(IEnumerableargs){IEnumeratorenumerator2=getAnotherEnumerator();foreach(vararginarg){enumerator2.MoveNext();yieldreturnarg+enumerator.Current;}}进入这个:boolMoveNext(){switch(this.state){case0:this.state=-1;this.enumerator2=get

c# - 如何使用 IEnumerator.Reset()?

到底怎样才是正确的调用方式IEnumerator.Reset?文档说:TheResetmethodisprovidedforCOMinteroperability.Itdoesnotnecessarilyneedtobeimplemented;instead,theimplementercansimplythrowaNotSupportedException.好的,那是否意味着我不应该调用它?使用异常进行流量控制是如此的诱人:using(enumerator=GetSomeExpensiveEnumerator()){while(enumerator.MoveNext()){...}t

c# - 如何使用 IEnumerator.Reset()?

到底怎样才是正确的调用方式IEnumerator.Reset?文档说:TheResetmethodisprovidedforCOMinteroperability.Itdoesnotnecessarilyneedtobeimplemented;instead,theimplementercansimplythrowaNotSupportedException.好的,那是否意味着我不应该调用它?使用异常进行流量控制是如此的诱人:using(enumerator=GetSomeExpensiveEnumerator()){while(enumerator.MoveNext()){...}t

c# - 为什么 Enumerable.Cast 引发 InvalidCastException?

如果我可以隐式地将整数值转换为double值,例如:inta=4;doubleb=a;//nowbholds4.0为什么我不能这样做:int[]intNumbers={10,6,1,9};double[]doubleNumbers2=intNumbers.Cast().ToArray();我收到“指定的转换无效”InvalidCastException异常。相反的操作(从double转换为int)会导致相同的错误。我做错了什么? 最佳答案 好吧,您对Cast的期望不正确,仅此而已-它旨在处理装箱/拆箱、引用和身份转换,仅此而已。不幸