C # Equivalent to Python itertools.chain

What (if any) is the C # equivalent for the itertools.chain Python method?

Python example:

l1 = [1, 2] l2 = [3, 4] for v in itertools.chain(l1, l2): print(v) 

Results:
1
2
3
4

Please note: I am not interested in creating a new list that combines my first two and then processes them. I want to save the memory / time that itertools.chain provides without instantiating this merged list.

+4
source share
2 answers

Enumerable.Concat ( MSDN )

 var l1 = new List<int>() { 1, 2 }; var l2 = new List<int>() { 3, 4 }; foreach(var item in Enumerable.Concat(l1, l2)) { Console.WriteLine(item.ToString()) } 
+3
source

You can do the same in C # using the Concat extension method from LINQ :

 l1.Concat(l2) 

LINQ uses a deferred execution model, so this will not create a new list.

+5
source

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


All Articles