Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using foreach loop to iterate through two lists

Tags:

I have two lists

List<object> a = new List<object>(); List<object> b = new List<object>(); 

Now i want to iterate through the elements of both list. I could do that by writing a foreach loop for each list. But is it also possible to do something like that?

foreach(object o in a, b) {  o.DoSomething(); } 

It would also be nice if something like that would be possible:

foreach (object o in a && b) {    o.DoSomething(); } 
like image 455
RoflcoptrException Avatar asked Dec 15 '10 13:12

RoflcoptrException


People also ask

How do you iterate through two lists in Python?

Use the izip() Function to Iterate Over Two Lists in Python It iterates over the lists until the smallest of them gets exhausted. It then zips or maps the elements of both lists together and returns an iterator object. It returns the elements of both lists mapped together according to their index.

Does foreach work with lists?

Using the CodeThe ForEach method of the List<T> (not IList<T> ) executes an operation for every object which is stored in the list. Normally it contains code to either read or modify every object which is in the list or to do something with list itself for every object.

How do I iterate over two arrays in JavaScript?

To use forEach to loop through two arrays at the same time in JavaScript, we can use the index parameter of the forEach callback to get the element with the same index from the 2nd array. const n = [1, 2, 3, 5, 7, 8, 9, 11, 12, 13]; const m = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; n.


1 Answers

foreach(object o in a.Concat(b)) {  o.DoSomething(); } 
like image 196
Itay Karo Avatar answered Oct 20 '22 16:10

Itay Karo