-3
foreach (var (counter, _, _) in Count())
{
    ... // do stuff with counter
}
return counter;

counter's out of scope after the foreach so the above doesn't work.

How can I use one of the iteration variables after a loop other than

int latestCounter;
foreach (var (counter, _, _) in Count())
{
    ... // do stuff with counter
    latestCounter = counter;
}
return latestCounter;
Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • 5
    What's wrong with your solution? – Jasen Nov 10 '21 at 02:01
  • Since `Count()` works in the `foreach` loop it must be returning an `IEnumerable<>` type. Why not just use `Last()` from `System.Linq` instead and do `return Count().Select(_ => _.Item1).Last();`? – Omar Abdel Bari Nov 10 '21 at 02:06
  • 3
    And this is an example of the XY problem, what are you trying to achieve? – Omar Abdel Bari Nov 10 '21 at 02:07
  • @OmarAbdelBari that iterates twice – theonlygusti Nov 10 '21 at 02:11
  • Unless there is code that you didn't show then no, this completely removes the need for a `foreach` loop. – Omar Abdel Bari Nov 10 '21 at 02:13
  • Ok now that it's updated. Why not just do it in the loop? That's the whole point of the loop. – Omar Abdel Bari Nov 10 '21 at 02:15
  • @OmarAbdelBari how can I know when I'm at the last item inside of the loop? – theonlygusti Nov 10 '21 at 02:15
  • This depends on what `Count()` returns. If it's an array or a list you can just store `Count()` result in a variable (ex `var values = Count()`) and then you can do `var lastValue = values[values.Count - 1]` if it's a `List` and if I recall correctly `var lastValue = values[values.Length - 1]` for arrays. If you are concerned about doing 2 iterations just make sure you return a type that doesn't need a second iteration. – Omar Abdel Bari Nov 10 '21 at 02:18
  • But in any case what you have will work for what you are trying to do. `latestCounter` will have the latest value and you can process `counter` within the `foreach` loop. – Omar Abdel Bari Nov 10 '21 at 02:38
  • If use while to replace foreach , and it can find out what is last, would you care about that ? – TimChang Nov 10 '21 at 05:17

1 Answers1

0

Call MoveNext() until last

    List<int> Count = new List<int>();
    IEnumerator<int> e = Count.GetEnumerator();
    if (e.MoveNext())
    {
        while (true)
        {
            var current = e.Current;
            bool anyNext = e.MoveNext();
            if (!anyNext)
            {
                // here in last
                break;
            }
        }
    }
        
TimChang
  • 2,249
  • 13
  • 25
  • 1
    This is pretty much where the question started: (effectively) a `foreach` loop that on each iteration assigns the iteration variable to a variable defined in a containing scope. – Lance U. Matthews Nov 10 '21 at 04:14
  • Regarding the edit, `Current` is undefined until `MoveNext()` is called. Also, if `Count` is empty then you'll never `break` out of the `while(true)`. I think you'll have to start with `if (e.MoveNext())` like the previous revision to solve both problems. – Lance U. Matthews Nov 10 '21 at 05:32
  • ok I edited my answer @LanceU.Matthews – TimChang Nov 10 '21 at 06:19