0

Goodmorning guys.

I'm coming from python environment and turning to c# scripting.

Is there a way in c# to simply collect multiple elements from a list as shown?

List = []
List.append(a,b,c,d,e,f,g,h,i)
items = List[2:6]

to get

c
d
e
f
g

Anyone got simple solutions?

  • You can look at the using System.Collections.Generic, and use the List<>. List is generic and will take any datatype for instance List my_list = new List(); And you can add to it like my_list.Add(2); And you can use a foreach to go through it. Or something like my_list.ForEach(x=> WriteLine($"x = {x} ")); – Omid CompSCI Jul 29 '16 at 17:58
  • Thank you guys. I solved like this, according to your advices: var Selected = list.Skip(2).Take(5); – Eduardo Pignatelli Aug 03 '16 at 13:04

2 Answers2

1

I think you want something like

var items = new [] {"a", "b", "c", "d", "e", "f", "g", "h", "i" };
foreach(var x in items.Skip(2).Take(5))
    Console.WriteLine(x);
juharr
  • 31,741
  • 4
  • 58
  • 93
1

Possibly equivalent of list comprehension in .NET is LINQ.

List newList = list.Skip(2).Take(6);

Try this to get your data.

Aren Hovsepyan
  • 1,947
  • 2
  • 17
  • 45