草庐IT

enumerators

全部标签

c# - 在空序列上使用 Enumerable.Aggregate(...) 方法

我想使用Enumerable.Aggregate(...)方法连接由分号分隔的字符串列表。很简单,不是吗?考虑以下因素:privateconststringLISTSEPARATOR=";";专辑。OrderedTracks是ListTrackDetails有DiscNumberInt16?属性(property)如果Distinct()返回的序列为空(因为Aggregate()方法不适用于空序列),以下语句将抛出异常:txtDiscNumber.Text=album.OrderedTracks.Where(a=>a.DiscNumber.HasValue).Select(a=>a.D

c# - Enumerable.Single 的错误执行?

我通过反射器在Enumerable.cs中发现了这个实现。publicstaticTSourceSingle(thisIEnumerablesource,Funcpredicate){//checkparametersTSourcelocal=default(TSource);longnum=0L;foreach(TSourcelocal2insource){if(predicate(local2)){local=local2;num+=1L;//Ithinktheyshoulddosomethingherelike://if(num>=2L)throwError.MoreThanOn

c# - Enumerator.MoveNext() 的奇怪行为

有人可以解释为什么这段代码在无限循环中运行吗?为什么MoveNext()总是返回true?varx=new{TempList=newList{1,3,6,9}.GetEnumerator()};while(x.TempList.MoveNext()){Console.WriteLine("HelloWorld");} 最佳答案 List.GetEnumerator()返回可变值类型(List.Enumerator)。您将该值存储在匿名类型中。现在,让我们看看它做了什么:while(x.TempList.MoveNext()){//I

c# - yield break 是否等同于从返回 IEnumerable<T> 的方法返回 Enumerable<T>.Empty

这两种方法对我来说似乎是一样的publicIEnumerableGetNothing(){returnEnumerable.Empty();}publicIEnumerableGetLessThanNothing(){yieldbreak;}我在测试场景中分析了每一个,我没有发现速度上有什么明显的差异,但是yieldbreak版本稍微快一些。是否有理由使用一个而不是另一个?一个比另一个更容易阅读吗?是否存在对调用者重要的行为差异? 最佳答案 如果你打算总是返回一个空的枚举然后使用Enumerable.Empty()语法更具声明性恕我

c# - 令人费解的 Enumerable.Cast InvalidCastException

以下抛出一个InvalidCastException。IEnumerablelist=newList(){1};IEnumerablecastedList=list.Cast();Console.WriteLine(castedList.First());为什么?我使用的是VisualStudio2008SP1。 最佳答案 这很奇怪!有一篇博文here描述了Cast()的行为在.NET3.5和.NET3.5SP1之间发生了变化,但它仍然没有解释InvalidCastException,如果您这样重写代码,您甚至会得到:varlist

c# - Resharper解释 "Possible multiple enumeration of IEnumerable"的示例代码

有时Resharper会警告:PossiblemultipleenumerationofIEnumerable有anSOquestiononhowtohandlethisissue,ReSharper站点也解释了一些事情here.它有一些示例代码告诉您改为执行此操作:IEnumerablenames=GetNames().ToList();我的问题是关于这个具体建议的:这不会仍然导致在2个for-each循环中对集合进行两次枚举吗? 最佳答案 GetNames()返回一个IEnumerable。因此,如果您存储该结果:IEnumer

c# - 为什么 Enumerable.Single() 迭代所有元素,即使已经找到多个元素?

在分析我们的一个应用程序时,我们发现某些代码出现神秘的减速,我们正在为一个大型集合调用Enumerable.Single(source,predicate),其中有多个项目与集合开始附近的谓词。调查显示theimplementationofEnumerable.Single()如下:publicstaticTSourceSingle(thisIEnumerablesource,Funcpredicate){TSourceresult=default(TSource);longcount=0;//NotehowthisalwaysiteratesthroughALLtheelements

c# - Enumerable.Empty<T>() 等效于 IQueryable

当方法返回IEnumerable时我没有任何东西可以返回,我们可以使用Enumerable.Empty().对于返回IQueryable的方法,是否有与上述等效的方法? 最佳答案 也许:Enumerable.Empty().AsQueryable(); 关于c#-Enumerable.Empty()等效于IQueryable,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/2691

c# - 使用 Enumerable.Empty<T>() 而不是 new List<T>() 来初始化 IEnumerable<T> 是否更好?

假设你有一个类Person:publicclassPerson{publicstringName{get;set;}publicIEnumerableRoles{get;set;}}我显然应该在构造函数中实例化角色。现在,我曾经用这样的列表来做:publicPerson(){Roles=newList();}但是我在System.Linq命名空间中发现了这个静态方法IEnumerableEnumerable.Empty();来自MSDN:TheEmpty(TResult)()methodcachesanemptysequenceoftypeTResult.Whentheobjectit

go - 在 Go 中使用位掩码(Go enumeration with iota)

卡住了。我正在尝试找出如何使用go枚举从const中获取位掩码值。比如key是5,也就是0101位,就是dog和fish。获取位值(1、2、4、8、16、32、64等)以便我可以选择字符串值并返回动物集的最简单方法是什么?typeKeyintconst(DogKey=1一直在阅读,但我无法解决这个问题。 最佳答案 声明一段字符串,其中字符串对应于常量名称:varanimalNames=[]string{"Dog","Cat","Fish","Horse","Snake","Rabbit","Lion","Rino","Hedgeho