草庐IT

ienumerable

全部标签

c# - 为什么有两个完全不同版本的 Reverse for List 和 IEnumerable?

对于List对象,我们有一个名为Reverse()的方法.它“就地”颠倒了列表的顺序,它不返回任何内容。对于IEnumerable对象,我们有一个名为Reverse()的扩展方法.它返回另一个IEnumerable。我需要倒序遍历一个列表,所以我不能直接使用第二种方法,因为我得到一个列表,我不想倒序,只是向后迭代。所以我可以这样做:for(inti=list.Count-1;i>=0;i--)或者foreach(variteminlist.AsEnumerable().Reverse())我发现它的可读性不如我有一个IEnumerable,就这样吧foreach(variteminli

c# - 异步迭代器任务<IEnumerable<T>>

我正在尝试实现一个返回迭代器的异步函数。思路如下:privateasyncTask>TestAsync(stringtestString){foreach(charcintestString.ToCharArray()){//dootherworkyieldreturnc;}}但是,有一个错误信息说这个函数不能是一个迭代器block,因为Task>不是迭代器接口(interface)类型。有解决办法吗? 最佳答案 Amore"batteries-included"implementation从C#8.0开始,包括语言支持在内的此类功

c# - 为什么不能将 IEnumerable<struct> 转换为 IEnumerable<object>?

为什么不允许最后一行?IEnumerabledoubleenumerable=newList{1,2};IEnumerablestringenumerable=newList{"a","b"};IEnumerableobjects1=stringenumerable;//OKIEnumerableobjects2=doubleenumerable;//Notallowed这是因为double不是从对象派生的值类型,因此协方差不起作用吗?这是否意味着没有办法使这项工作:publicinterfaceIMyInterface{stringMethod();}publicclassMyCla

c# - 具有键 'CategoryId' 的 ViewData 项的类型为 'System.Int32' 但必须为 'IEnumerable<SelectListItem>' 类型?

所以我的代码之前是有效的。我不知道我做了什么让这件事发生,而且我似乎无法修复它。我见过有人说要重置ModelState。(ModelState.Clear();)但这并没有帮助。此外,我对MVC还很陌生,这也无济于事。任何帮助,将不胜感激。谢谢。Controller:publicActionResultCreate(){ActiveDirectoryModeladm=newActiveDirectoryModel();ViewBag.notifyto=adm.FetchContacts();varmodel=Populate();returnView(model);}[HttpPost

c# - IEnumerable<T> 作为返回类型

使用IEnumerable有问题吗?作为返回类型?FxCop提示返回List(它建议改为返回Collection)。好吧,我一直遵循一条规则“接受最少的,但返回最多的。”由此看来,返回IEnumerable是一件坏事,但是当我想使用“惰性检索”时该怎么办?此外,yield关键字真是个好东西。 最佳答案 这实际上是一个由两部分组成的问题。1)返回IEnumerable本质上有什么问题吗?什么都没有。事实上,如果您使用C#迭代器,这是预期的行为。先发制人地将其转换为List或其他集合类并不是一个好主意。这样做是对调用者的使用模式做出假设

c# - IEnumerable<T> 跳过无限序列

我有一个使用BigInteger的斐波那契数列的简单实现:internalclassFibonacciEnumerator:IEnumerator{privateBigInteger_previous=1;privateBigInteger_current=0;publicvoidDispose(){}publicboolMoveNext(){returntrue;}publicvoidReset(){_previous=1;_current=0;}publicBigIntegerCurrent{get{vartemp=_current;_current+=_previous;_pre

c# - 实现 IEnumerable<T> 时遇到问题

我正在尝试编写我自己的(简单的)List实现。这是我到目前为止所做的:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceprovaIEnum{classMyList:IEnumerable{privateT[]_array;publicintCount{get;privateset;}publicMyList(){/*...*/}publicvoidAdd(Telement){/*...*/}//...publicIEnumeratorGetEnumerator(

c# - 如何初始化为空的 IEnumerable<Object> 并允许对其进行 Concat?

我试过将b添加到books的代码:IEnumerablebooks=null;foreach(Bookbincontext.Books.AsEnumerable())if(someConditions)books=books.Concat(new[]{b});但是在代码的最后一行给我这个错误:System.ArgumentNullException:Valuecannotbenull.Parametername:firstnullCollection似乎无法串联。我使用EF,那么我应该如何初始化我的Collection,其中没有任何东西并且我可以连接到它?

c# - 我应该支持 IEnumerable<T> 还是数组?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭6年前。Improvethisquestion在我从事的许多项目中,每当我必须返回只读集合时,我都会使用IEnumerable接口(interface)并使其类型特定,如下所示:PublicReadOnlyPropertyGetValues()AsIEnumerable(OfInteger)Get'codetoreturnthevalues'EndGetEndProperty大多数时候,我会返回一个列表,但在某些函数和只读属性中,我会

c# - yield return 仅适用于 IEnumerable<T>?

我可以使用yieldreturn吗?当返回类型是IGrouping时或IDictionary? 最佳答案 yieldreturn恰好适用于4种情况:IEnumerableIEnumerableIEnumeratorIEnumerator这是因为它要在内部建立一个状态机;字典(等)用这个是不可能的。你当然可以只return一个合适的类型。 关于c#-yieldreturn仅适用于IEnumerable?,我们在StackOverflow上找到一个类似的问题: h