471,850 Members | 1,233 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,850 software developers and data experts.

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.

Marc

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 1639

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 YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.