When, if at all, should IEnumerable be used over IEnumerable?

Never, as far as I can see. IEnumerable inherits IEnumerable, so exposing IEnumerable communicates what you know about T to the caller, while still allowing functions written before generics to use the collection. The reason non-generic IEnumerable exists is historical.

Generally, I implement IEnumerable.GetEnumerator() as an explicit interface implementation, and have IEnumerable.GetEnumerator() as the implicit implementation. That way, it is still backwards compatbile (for non-generic code) but using the class normally is fully generic – thecoop Jul 14 '09 at 14:33.

The only time I've found where I would use IEnumerable over IEnumerable is when dealing with LINQ queries and anonymous objects. Consider the following (contrived) example: class Person { public string FirstName { get; set; } public string LastName { get; set; } } IEnumerable GetFirstNames(IEnumerable people) { return from p in people select new { Name = p. FirstName }; } This will generate the following error: error CS0266: Cannot implicitly convert type 'System.Collections.Generic.

IEnumerable' to 'System.Collections.Generic. IEnumerable'. An explicit conversion exists (are you missing a cast?) However, if you change the return type of GetFirstNames() to IEnumerable, it will compile.

1 I would personally prefer to leave the signature as IEnumerable, and change the code to select new { Name = p. FirstName }.Cast(); – mquander Jul 14 '09 at 14:24.

If you don't have generics available, i.e. You are using . NET 1.1.

Perhaps, not as a return value. Otherwise, use it whenever possible -- if you can get away with it. It'll make your code more general since IEnumerable is a more general IEnumerable.

Foor example, consider a general function that just prints the nun-null values of an IEnumerable.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions