码迷,mamicode.com
首页 > 其他好文 > 详细

Generation 操作

时间:2017-02-06 23:55:06      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:code   starting   each   val   cti   rom   range   class   ble   

Empty

Returns an empty collection

  1. var emptyCollection1 =Enumerable.Empty<string>();
  2. var emptyCollection2 =Enumerable.Empty<Student>();
  3. Console.WriteLine("Count: {0} ", emptyCollection1.Count());
  4. Console.WriteLine("Type: {0} ", emptyCollection1.GetType().Name);
  5. Console.WriteLine("Count: {0} ",emptyCollection2.Count());
  6. Console.WriteLine("Type: {0} ", emptyCollection2.GetType().Name);
  7. Results:
  8. Type:String[]
  9. Count:0
  10. Type:Student[]
  11. Count:0

Range

Generates collection of IEnumerable type with specified number of elements with sequential values, starting from first element.

  1. var intCollection =Enumerable.Range(10,10);
  2. Console.WriteLine("Total Count: {0} ", intCollection.Count());
  3. for(int i =0; i < intCollection.Count(); i++)
  4. Console.WriteLine("Value at index {0} : {1}", i, intCollection.ElementAt(i));
  5. Results:
  6. TotalCount:10
  7. Value at index 0:10
  8. Value at index 1:11
  9. Value at index 2:12
  10. Value at index 3:13
  11. Value at index 4:14
  12. Value at index 5:15
  13. Value at index 6:16
  14. Value at index 7:17
  15. Value at index 8:18
  16. Value at index 9:19

Repeat

Generates a collection of IEnumerable type with specified number of elements and each element contains same specified value.

  1. var intCollection =Enumerable.Repeat<int>(10,10);
  2. Console.WriteLine("Total Count: {0} ", intCollection.Count());
  3. for(int i =0; i < intCollection.Count(); i++)
  4. Console.WriteLine("Value at index {0} : {1}", i, intCollection.ElementAt(i));
  5. Results:
  6. TotalCount:10
  7. Value at index 0:10
  8. Value at index 1:10
  9. Value at index 2:10
  10. Value at index 3:10
  11. Value at index 4:10
  12. Value at index 5:10
  13. Value at index 6:10
  14. Value at index 7:10
  15. Value at index 8:10
  16. Value at index 9:10

Generation 操作

标签:code   starting   each   val   cti   rom   range   class   ble   

原文地址:http://www.cnblogs.com/wuyicqb/p/6371936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!