469,575 Members | 1,193 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

Re: List<T> constructor taking IEnumerable<T> behaves surprisingly

You could bypass the List<T>'s tendency to use ICollection<Tby
simply not giving it an ICollection<T- for example, with the C# 3
extension method below you could use:

List<Tlist = new List<T>(source.AsEnumerableOnly());

The AsEnumerableOnly() *only* exposes IEnumerable<T(OK, it also
exposes IEnumerable and IDisposable ;-p), allowing your non-Count-
friendly implementation to work happily.


public static class EnumerableExt
public static IEnumerable<TAsEnumerableOnly<T>(
this IEnumerable<Tsource)
if (source == null) yield break; // GIGO
foreach (T item in source)
yield return item;
Jun 27 '08 #1
0 1544

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by PJ | last post: by
reply views Thread by Iron Moped | last post: by
7 posts views Thread by Andrew Robinson | last post: by
44 posts views Thread by Zytan | last post: by
2 posts views Thread by Fred Mellender | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.