C # Variable Manipulation

If i have

List<String> text

how can I create a sub-list of all contiguous elements in a certain range, for example.

List<String> subList = /* all elements within text bar the first 2*/

Also are there any helpful helpful tips and tricks that might be helpful?

+3
source share
3 answers

This will work even without LINQ:

List<String> subList = text.GetRange(2, text.Count - 2);

Edit: Fixed a typo.

+12
source
subList = text.Skip(2).ToList()

Skip (n) returns an IEnumerable <> with all elements except the first n.

When you really need a list after this, ToList () converts it back.

+8
source

Source: https://habr.com/ru/post/1702739/


All Articles