C
C#β€’2y ago
Gibin

❔ C# forloops

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
15 Replies
TheBoxyBear
TheBoxyBearβ€’2y ago
Like process one loan then one returned then one loan etc?
Gibin
Gibinβ€’2y ago
yes
TheBoxyBear
TheBoxyBearβ€’2y ago
https://stackoverflow.com/questions/20141241/selecting-alternate-items-of-an-array-c-sharp Similar to this, but you would need to combine both collections first with Concat
Stack Overflow
Selecting alternate items of an array C#
I have an array statsname as apple X banana Y Kiwi z I need to put apple,banana and Kiwi in an array Fruits and X,Y and Z in an array called alphabets. Any simple C# mechanism for it please ?
TheBoxyBear
TheBoxyBearβ€’2y ago
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
Gibin
Gibinβ€’2y ago
how do i implement this
TheBoxyBear
TheBoxyBearβ€’2y ago
using var loanEnumerator = loans.GetEnumerator() And do the same for returns
Gibin
Gibinβ€’2y ago
okay thank you
TheBoxyBear
TheBoxyBearβ€’2y ago
You just have to check the return value of MoveNext every time When it returns false, that means it was already pointing at the last item before the call
Gibin
Gibinβ€’2y ago
yesss
TheBoxyBear
TheBoxyBearβ€’2y ago
Or there are no items. The enumerator starts before the first item so you have to start with MoveNext
Gibin
Gibinβ€’2y ago
okay
TheBoxyBear
TheBoxyBearβ€’2y ago
And don't forget the using πŸ™‚ no brackets needed in modern c# Like with the StreamWriter 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 Then it's as simple as foreach (var item in listA.Alternate(listB)) public static AlternatingEnumerable<T> Alternate(this IEnumerable<T> first, params IEnumerable<T>[] others)
Thinker
Thinkerβ€’2y ago
Btw you probably want to use properties instead of all those GetX methods.
risanha
risanhaβ€’2y ago
^ this. properties are the elegant solution to getters and setters in languages like java
Accord
Accordβ€’2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
βœ… Using Dependency Injection, sharing singletons between builders?I'm not sure if I'm taking a completely off-the-wall approach to this, so please let me know if I sh❔ hook errors and unexpected }So I’m working on some code for rust as of now and it’s function so far is to spawn a crate at a pla❔ Tabbing between Entries or Editors in a gridI've got a grid with Entries, I'm trying to use the Tab key to move to the next Entry but it just fo❔ Unit Testing: How to set up HttpRuntime.BinDirectory to prevent ArgumentNullException?I am trying to make a unit test that calls code that uses HttpRuntime.BinDirectory, it is throwing S❔ Need some help for codingThere are an eye tracker device and a frame that created in XAML. What I want to do is move the fram❔ can any one explain the codeβœ… When using Dependency Injection, how can I pass other parameters into the constructor?I'm just starting to get my head around the basics of DI. I've got a number of instances that were b❔ Package the binaries of another projectWe have two projects in a solution, A and B. We'd like B to ship the binaries of A when packaged (no❔ Compile Project With Legacy C# 7 Preview (from 2016) FeaturesHey guys I have an old project that extensively uses C# 7 Preview features from 2016. Anyone knows a❔ different variable type depending on condition of IF statementHi there, how would I go about having a different type on a variable with the same name, for later u