By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,903 Members | 1,670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,903 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.