ienumerable

What concrete type does 'yield return' return?

早过忘川 提交于 2019-12-30 08:26:10
问题 What is the concrete type for this IEnumerable<string> ? private IEnumerable<string> GetIEnumerable() { yield return "a"; yield return "a"; yield return "a"; } 回答1: It's a compiler-generated type. The compiler generates an IEnumerator<string> implementation that returns three "a" values and an IEnumerable<string> skeleton class that provides one of these in its GetEnumerator method. The generated code looks something like this*: // No idea what the naming convention for the generated class is

F# and interface covariance: what to do? (specifically seq<> aka IEnumerable<>)

99封情书 提交于 2019-12-30 05:59:25
问题 I'm trying to call a .NET method accepting a generic IEnumerable<T> from F# using a seq<U> such that U is a subclass of T. This doesn't work the way I expected it would: With the following simple printer: let printEm (os: seq<obj>) = for o in os do o.ToString() |> printfn "%s" These are the results I get: Seq.singleton "Hello World" |> printEm // error FS0001; //Expected seq<string> -> 'a but given seq<string> -> unit Seq.singleton "Hello World" :> seq<obj> |> printEm // error FS0193; //seq

How to loop through a collection that supports IEnumerable?

北战南征 提交于 2019-12-29 13:35:39
问题 How to loop through a collection that supports IEnumerable? 回答1: A regular for each will do: foreach (var item in collection) { // do your stuff } 回答2: Along with the already suggested methods of using a foreach loop, I thought I'd also mention that any object that implements IEnumerable also provides an IEnumerator interface via the GetEnumerator method. Although this method is usually not necessary, this can be used for manually iterating over collections, and is particularly useful when

IEnumerable<T> null coalescing Extension

本秂侑毒 提交于 2019-12-29 08:42:28
问题 I frequently face the problem to check whether an IEnumerable<T> is null before iterating over it through foreach or LINQ queries, then I often come into codes like this: var myProjection = (myList ?? Enumerable.Empty<T>()).Select(x => x.Foo)... Hence, I thought to add this extension-method to an Extensions class: public static class MyExtensions { public static IEnumerable<T> AsEmptyIfNull<T>(this IEnumerable<T> source) { return source ?? Enumerable.Empty<T>(); } } A little issue comes

How do I implement IEnumerable?

被刻印的时光 ゝ 提交于 2019-12-29 06:24:33
问题 I have a class that contains a static number of objects. This class needs to be frequently 'compared' to other classes that will be simple List objects. public partial class Sheet { public Item X{ get; set; } public Item Y{ get; set; } public Item Z{ get; set; } } the items are obviously not going to be "X" "Y" "Z", those are just generic names for example. The problem is that due to the nature of what needs to be done, a List won't work; even though everything in here is going to be of type

Why can't I assign List<int> to IEnumerable<object> in .NET 4.0

元气小坏坏 提交于 2019-12-28 20:37:32
问题 I try to do this: IEnumerable<object> ids = new List<string>() { "0001", "0002", "0003" }; it works great! But when I try to do this: IEnumerable<object> intIds = new List<System.Int32>() { 1, 2, 3 }; Visual Studio tells me: Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) Why is that? 回答1: int is a value type and can only be boxed to object - it doesn't inherit from object .

Does Class need to implement IEnumerable to use Foreach

心已入冬 提交于 2019-12-28 14:31:43
问题 This is in C#, I have a class that I am using from some else's DLL. It does not implement IEnumerable but has 2 methods that pass back a IEnumerator. Is there a way I can use a foreach loop on these. The class I am using is sealed. 回答1: foreach does not require IEnumerable , contrary to popular belief. All it requires is a method GetEnumerator that returns any object that has the method MoveNext and the get-property Current with the appropriate signatures. /EDIT: In your case, however, you're

Is it possible to turn an IEnumerable into an IOrderedEnumerable without using OrderBy?

こ雲淡風輕ζ 提交于 2019-12-28 14:02:21
问题 Say there is an extension method to order an IQueryable based on several types of Sorting (i.e. sorting by various properties) designated by a SortMethod enum. public static IOrderedEnumerable<AClass> OrderByX(this IQueryable<AClass> values, SortMethod? sortMethod) { IOrderedEnumerable<AClass> queryRes = null; switch (sortMethod) { case SortMethod.Method1: queryRes = values.OrderBy(a => a.Property1); break; case SortMethod.Method2: queryRes = values.OrderBy(a => a.Property2); break; case null

IEnumerable<T> as return type

爱⌒轻易说出口 提交于 2019-12-28 02:49:07
问题 Is there a problem with using IEnumerable<T> as a return type? FxCop complains about returning List<T> (it advises returning Collection<T> instead). Well, I've always been guided by a rule "accept the least you can, but return the maximum." From this point of view, returning IEnumerable<T> is a bad thing, but what should I do when I want to use "lazy retrieval"? Also, the yield keyword is such a goodie. 回答1: This is really a two part question. 1) Is there inherently anything wrong with

How to loop through IEnumerable in batches [duplicate]

耗尽温柔 提交于 2019-12-27 11:01:43
问题 This question already has answers here : Create batches in linq (16 answers) Closed 4 months ago . I am developing a c# program which has an "IEnumerable users" that stores the ids of 4 million users. I need to loop through the Ienummerable and extract a batch 1000 ids each time to perform some operations in another method. How do I extract 1000 ids at a time from start of the Ienumerable ...do some thing else then fetch the next batch of 1000 and so on ? Is this possible? 回答1: Sounds like