Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 0.50 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to get an empty list of a collection?
  2. var result = MyCollection.Take(0).ToList();
  3.  
  4. var result = MyCollection.Where(p => false).ToList();
  5.        
  6. public static IList<T> CreateEmptyCopy(this IEnumerable<T> source)
  7. {
  8.    return source.Take(0).ToList();
  9. }
  10.        
  11. var result = MyCollection.CreateEmptyCopy();
  12.        
  13. public static IList<T> GetEmptyList<T>(this IEnumerable<T> source)
  14. {
  15.     return new List<T>();
  16. }
  17.  
  18. var emp = MyCollection.GetEmptyList();
  19.        
  20. Enumerable.Empty<T>();
  21.        
  22. Enumerable.Empty<T>().ToList<T>();