首页 > 代码库 > Generation 操作
Generation 操作
Empty
Returns an empty collection
var emptyCollection1 =Enumerable.Empty<string>();
var emptyCollection2 =Enumerable.Empty<Student>();
Console.WriteLine("Count: {0} ", emptyCollection1.Count());
Console.WriteLine("Type: {0} ", emptyCollection1.GetType().Name);
Console.WriteLine("Count: {0} ",emptyCollection2.Count());
Console.WriteLine("Type: {0} ", emptyCollection2.GetType().Name);
Results:
Type:String[]
Count:0
Type:Student[]
Count:0
Range
Generates collection of IEnumerable type with specified number of elements with sequential values, starting from first element.
var intCollection =Enumerable.Range(10,10);
Console.WriteLine("Total Count: {0} ", intCollection.Count());
for(int i =0; i < intCollection.Count(); i++)
Console.WriteLine("Value at index {0} : {1}", i, intCollection.ElementAt(i));
Results:
TotalCount:10
Value at index 0:10
Value at index 1:11
Value at index 2:12
Value at index 3:13
Value at index 4:14
Value at index 5:15
Value at index 6:16
Value at index 7:17
Value at index 8:18
Value at index 9:19
Repeat
Generates a collection of IEnumerable type with specified number of elements and each element contains same specified value.
var intCollection =Enumerable.Repeat<int>(10,10);
Console.WriteLine("Total Count: {0} ", intCollection.Count());
for(int i =0; i < intCollection.Count(); i++)
Console.WriteLine("Value at index {0} : {1}", i, intCollection.ElementAt(i));
Results:
TotalCount:10
Value at index 0:10
Value at index 1:10
Value at index 2:10
Value at index 3:10
Value at index 4:10
Value at index 5:10
Value at index 6:10
Value at index 7:10
Value at index 8:10
Value at index 9:10
Generation 操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。