0Day Forums
foreach with index - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: C# (https://zeroday.vip/Forum-C)
+--- Thread: foreach with index (/Thread-foreach-with-index)



foreach with index - caulescent149849 - 07-24-2023

Is there a C# equivalent of Python's `enumerate()` and Ruby's `each_with_index`?



RE: foreach with index - semblative181952 - 07-24-2023

It depends on the class you are using.

Dictionary<(Of <(TKey, TValue>)>) Class For Example Support This

The Dictionary<(Of <(TKey, TValue>)>) generic class provides a mapping from a set of keys to a set of values.

For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair<(Of <(TKey, TValue>)>) structure representing a value and its key. The order in which the items are returned is undefined.

foreach (KeyValuePair<int, string> kvp in myDictionary) {...}



RE: foreach with index - Sirjacobhywiuvl - 07-24-2023

My solution involves a simple Pair<X, Y> class I created for general utility, and which is operationally essentially the same as the framework class KeyValuePair<X, Y>. Then I created a couple extension functions for IEnumerable<T> called Ordinate (from the set theory term "[ordinal][1]").

These functions will return for each item a Pair object containing the index, and the item itself.

public static IEnumerable<Pair<Int32, X>> Ordinate<X>(this IEnumerable<X> lhs)
{
return lhs.Ordinate(0);
}

public static IEnumerable<Pair<Int32, X>> Ordinate<X>(this IEnumerable<X> lhs, Int32 initial)
{
Int32 index = initial - 1;

return lhs.Select(x => new Pair<Int32, X>(++index, x));
}


[1]:

[To see links please register here]




RE: foreach with index - emplane905 - 07-24-2023

This is your collection

var values = new[] {6, 2, 8, 45, 9, 3, 0};

Make a range of indexes for this collection

var indexes = Enumerable.Range(0, values.Length).ToList();

Use the range to iterate with index

indexes.ForEach(i => values[i] += i);
indexes.ForEach(i => Console.Write("[{0}] = {1}", i, values[i]));



RE: foreach with index - ardythdogmubupwf - 07-24-2023

I like being able to use foreach, so I made an extension method and a structure:

public struct EnumeratedInstance<T>
{
public long cnt;
public T item;
}

public static IEnumerable<EnumeratedInstance<T>> Enumerate<T>(this IEnumerable<T> collection)
{
long counter = 0;
foreach (var item in collection)
{
yield return new EnumeratedInstance<T>
{
cnt = counter,
item = item
};
counter++;
}
}

and an example use:

foreach (var ii in new string[] { "a", "b", "c" }.Enumerate())
{
Console.WriteLine(ii.item + ii.cnt);
}

One nice thing is that if you are used to the Python syntax, you can still use it:

foreach (var ii in Enumerate(new string[] { "a", "b", "c" }))


RE: foreach with index - stygial267088 - 07-24-2023

No, there is not.

As other people have shown, there are ways to simulate Ruby's behavior. But it is possible to have a type that implements [IEnumerable][1] that does not expose an index.

[1]:

[To see links please register here]