草庐IT

IEnumerable

全部标签

c# - 在 List<T> 和 IEnumerable<T> 之间自由转换

如何转换List到IEnumerable然后又回来了?我想这样做是为了在列表上运行一系列LINQ语句,例如。G。Sort() 最佳答案 ListmyList=newList();IEnumerablemyEnumerable=myList;ListlistAgain=myEnumerable.ToList(); 关于c#-在List和IEnumerable之间自由转换,我们在StackOverflow上找到一个类似的问题: https://stackoverf

c# - 可以通过 foreach 向后迭代吗?

我知道我可以使用for语句并达到相同的效果,但我可以在C#中通过foreach循环向后循环吗? 最佳答案 如果您使用的是.NET3.5,您可以这样做:IEnumerableenumerableThing=...;foreach(varxinenumerableThing.Reverse())它不是很有效,因为它基本上必须通过枚举器向前将所有内容放入堆栈,然后以相反的顺序将所有内容弹出。如果您有一个可直接索引的集合(例如IList),您绝对应该使用for循环。如果您使用的是.NET2.0并且不能使用for循环(即您只有一个IEnume

c# - 可以通过 foreach 向后迭代吗?

我知道我可以使用for语句并达到相同的效果,但我可以在C#中通过foreach循环向后循环吗? 最佳答案 如果您使用的是.NET3.5,您可以这样做:IEnumerableenumerableThing=...;foreach(varxinenumerableThing.Reverse())它不是很有效,因为它基本上必须通过枚举器向前将所有内容放入堆栈,然后以相反的顺序将所有内容弹出。如果您有一个可直接索引的集合(例如IList),您绝对应该使用for循环。如果您使用的是.NET2.0并且不能使用for循环(即您只有一个IEnume

c# - 我如何实现 IEnumerable<T>

我知道如何实现非通用IEnumerable,如下所示:usingSystem;usingSystem.Collections;namespaceConsoleApplication33{classProgram{staticvoidMain(string[]args){MyObjectsmyObjects=newMyObjects();myObjects[0]=newMyObject(){Foo="Hello",Bar=1};myObjects[1]=newMyObject(){Foo="World",Bar=2};foreach(MyObjectxinmyObjects){Conso

c# - 我如何实现 IEnumerable<T>

我知道如何实现非通用IEnumerable,如下所示:usingSystem;usingSystem.Collections;namespaceConsoleApplication33{classProgram{staticvoidMain(string[]args){MyObjectsmyObjects=newMyObjects();myObjects[0]=newMyObject(){Foo="Hello",Bar=1};myObjects[1]=newMyObject(){Foo="World",Bar=2};foreach(MyObjectxinmyObjects){Conso

c# - 用于公开成员集合的 ReadOnlyCollection 或 IEnumerable?

如果调用代码仅遍历集合,是否有任何理由将内部集合公开为ReadOnlyCollection而不是IEnumerable?classBar{privateICollectionfoos;//Whichoneistobepreferred?publicIEnumerableFoos{...}publicReadOnlyCollectionFoos{...}}//Callingcode:foreach(varfinbar.Foos)DoSomething(f);在我看来,IEnumerable是ReadOnlyCollection接口(interface)的一个子集,它不允许用户修改集合。因

c# - 用于公开成员集合的 ReadOnlyCollection 或 IEnumerable?

如果调用代码仅遍历集合,是否有任何理由将内部集合公开为ReadOnlyCollection而不是IEnumerable?classBar{privateICollectionfoos;//Whichoneistobepreferred?publicIEnumerableFoos{...}publicReadOnlyCollectionFoos{...}}//Callingcode:foreach(varfinbar.Foos)DoSomething(f);在我看来,IEnumerable是ReadOnlyCollection接口(interface)的一个子集,它不允许用户修改集合。因

c# - 为什么这个字符串扩展方法没有抛出异常?

我有一个应该返回IEnumerable的C#字符串扩展方法字符串中子字符串的所有索引。它完美地实现了预期目的,并返回了预期的结果(正如我的一个测试所证明的,尽管不是下面的那个),但另一个单元测试发现了它的一个问题:它无法处理空参数。这是我正在测试的扩展方法:publicstaticIEnumerableAllIndexesOf(thisstringstr,stringsearchText){if(searchText==null){thrownewArgumentNullException("searchText");}for(intindex=0;;index+=searchText

c# - 为什么这个字符串扩展方法没有抛出异常?

我有一个应该返回IEnumerable的C#字符串扩展方法字符串中子字符串的所有索引。它完美地实现了预期目的,并返回了预期的结果(正如我的一个测试所证明的,尽管不是下面的那个),但另一个单元测试发现了它的一个问题:它无法处理空参数。这是我正在测试的扩展方法:publicstaticIEnumerableAllIndexesOf(thisstringstr,stringsearchText){if(searchText==null){thrownewArgumentNullException("searchText");}for(intindex=0;;index+=searchText

c# - 如何将 IEnumerable 转换为 ObservableCollection?

如何将IEnumerable转换为ObservableCollection? 最佳答案 根据MSDNvarmyObservableCollection=newObservableCollection(myIEnumerable);这将生成当前IEnumerable的浅拷贝并将其转换为ObservableCollection。 关于c#-如何将IEnumerable转换为ObservableCollection?,我们在StackOverflow上找到一个类似的问题: