is there a way i can let 2 for loops run but then switching turns kinda, For example if the first forloop only outputs 1s and the second one only 2s, i want the output to be 121212 instead of 111222
TheBoxyBear1/11/2023
Like process one loan then one returned then one loan etc?
For better performance, you can also do low level enumeration by getting the enumerator from each collection. The enumerator has a MoveNext method to move to the next item and a Current property to get the item it's currently pointing at
Gibin1/11/2023
how do i implement this
TheBoxyBear1/11/2023
using var loanEnumerator = loans.GetEnumerator()
TheBoxyBear1/11/2023
And do the same for returns
Gibin1/11/2023
okay thank you
TheBoxyBear1/11/2023
You just have to check the return value of MoveNext every time
TheBoxyBear1/11/2023
When it returns false, that means it was already pointing at the last item before the call
Gibin1/11/2023
yesss
TheBoxyBear1/11/2023
Or there are no items. The enumerator starts before the first item so you have to start with MoveNext
Gibin1/11/2023
okay
TheBoxyBear1/11/2023
And don't forget the using 🙂 no brackets needed in modern c#
TheBoxyBear1/11/2023
Like with the StreamWriter
TheBoxyBear1/11/2023
I had this problem once and made a custom collection class that takes multiple enumerables and uses a custom enumerator that handles all the alternating logic when MoveNext is called
TheBoxyBear1/11/2023
Then it's as simple as foreach (var item in listA.Alternate(listB))
TheBoxyBear1/11/2023
public static AlternatingEnumerable<T> Alternate(this IEnumerable<T> first, params IEnumerable<T>[] others)
thinker2271/11/2023
Btw you probably want to use properties instead of all those GetX methods.
Risa1/11/2023
^ this. properties are the elegant solution to getters and setters in languages like java