Reputation: 358
In C++, one can initialize a vector as
vector<int> nums1 (100, 4); // 100 integers with value 4
In additions, there is something like
vector<int> nums2 (nums1.begin() + 5, nums1.end() - 20);
Is there something similar in C# for List?
Upvotes: 0
Views: 791
Reputation: 37095
First one goes this:
var result = Enumerable.Repeat(4, 100);
Second one (I´m not familiar with C++) would be this ( I suppose it means something like take elements from the fith until the 20th from the end):
var result2 = result.Skip(5).Take(75);
Be aware that both result
and result2
are just iterators and thus are lazily evaluated. Calling ToList
or ToArray
will actually materialize the collection by executing the query.
Upvotes: 0
Reputation: 1
You can do this like this by List.
using System.Collections.Generic;
List<int> nums1 = new List<int>(Enumerable.Repeat(4,100)); //100 elements each with value 4
List<int> nums2 = new List<int>(nums1.Skip(5).Take(75)); // skip first 5 and take 75 elements
Upvotes: 1