Is there a C# equivalent of Python's enumerate()
and Ruby's each_with_index
?
10 Answers
I keep this extension method around for this:
public static void Each<T>(this IEnumerable<T> ie, Action<T, int> action)
{
var i = 0;
foreach (var e in ie) action(e, i++);
}
And use it like so:
var strings = new List<string>();
strings.Each((str, n) =>
{
// hooray
});
Or to allow for break
-like behaviour:
public static bool Each<T>(this IEnumerable<T> ie, Func<T, int, bool> action)
{
int i = 0;
foreach (T e in ie) if (!action(e, i++)) return false;
return true;
}
var strings = new List<string>() { "a", "b", "c" };
bool iteratedAll = strings.Each ((str, n)) =>
{
if (str == "b") return false;
return true;
});
-
30
-
29
-
6@TriQ That's just another reason to prefer it, most of the time. :) Jun 14, 2012 at 12:58
-
1@DanFinch I wasn't being snarky, I was legitimately looking for an answer. Aug 11, 2012 at 3:02
-
2@TankorSmash It's an "extension method". Look this up. Bit of awesome C# magic. Nov 5, 2013 at 21:32
You can do the following
foreach (var it in someCollection.Select((x, i) => new { Value = x, Index = i }) )
{
if (it.Index > SomeNumber) //
}
This will create an anonymous type value for every entry in the collection. It will have two properties
Value
: with the original value in the collectionIndex
: with the index within the collection
-
22Clever, but it's like scratching your left ear with your right hand. I guess I'll just keep the index myself, so I don't confuse future maintainers.– KenFeb 6, 2009 at 19:32
-
41@Neil, I'm amazed that people think this is a maintenence problem. This overload of Select (and other LINQ methods) was provided for the complete purpose of doing operations like this.– JaredParFeb 6, 2009 at 19:57
-
4I see no issues with maintenance. If a future maintainer is incapable of going to the MSDN documentation and looking up the overloads to the Select method, that is their own problem. Worried about the variable names? Just do: (Value, Index) => select new { Value, Index } for the lambda. Jan 21, 2011 at 14:49
-
10@Lodewijk Without a backing collection,
IEnumerable<T>.ElementAt(i)
is O(n) (see Schlemiel the Painter). And what if you have a lazy enumerable that's expensive to evaluate and contains a huge number of records? With a for loop, you have to wait forIEnumerable<T>.Count()
to return before you can start processing the records. A for loop is generally not appropriate for use withIEnumerable<T>
.– piedarAug 4, 2014 at 19:01 -
8This can now get a bit shorter when using a C# 7 ValueTuple:
foreach (var (x, i) in someCollection.Select((x, i) => (x, i)) ) { ... }
– FernAndrSep 17, 2019 at 12:20
The C# foreach doesn't have a built in index. You'll need to add an integer outside the foreach loop and increment it each time.
int i = -1;
foreach (Widget w in widgets)
{
i++;
// do something
}
Alternatively, you could use a standard for loop as follows:
for (int i = 0; i < widgets.Length; i++)
{
w = widgets[i];
// do something
}
-
19I think you should initialize i to -1 and increment it at the beginning of the loop body to make sure that a "continue" statement doesn't cause problems. Feb 6, 2009 at 19:11
-
7@DrJokepu why don't simply keep i = 0 and increment it before the closing bracket of the foreach statement? Initializing with something != 0 triggers the attention when you later browse the code...– AdiAug 23, 2012 at 9:32
-
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" }))
-
-
Sorry for necropost. I moddified your function so it looks pretty and more pythonic in use: pastebin.com/aExvenyY Jan 5, 2017 at 1:10
Aside from the LINQ answers already given, I have a "SmartEnumerable" class which allows you to get the index and the "first/last"-ness. It's a bit ugly in terms of syntax, but you may find it useful.
We can probably improve the type inference using a static method in a nongeneric type, and implicit typing will help too.
-
2Great! These small little helper properties (first/last/index) should be included in the standard .net framework! Apr 19, 2010 at 16:08
-
My solution involves a simple Pair class I created for general utility, and which is operationally essentially the same as the framework class KeyValuePair. Then I created a couple extension functions for IEnumerable called Ordinate (from the set theory term "ordinal").
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));
}
-
2
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 that does not expose an index.
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]));
I just figured out interesting solution:
public class DepthAware<T> : IEnumerable<T>
{
private readonly IEnumerable<T> source;
public DepthAware(IEnumerable<T> source)
{
this.source = source;
this.Depth = 0;
}
public int Depth { get; private set; }
private IEnumerable<T> GetItems()
{
foreach (var item in source)
{
yield return item;
++this.Depth;
}
}
public IEnumerator<T> GetEnumerator()
{
return GetItems().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
// Generic type leverage and extension invoking
public static class DepthAware
{
public static DepthAware<T> AsDepthAware<T>(this IEnumerable<T> source)
{
return new DepthAware<T>(source);
}
public static DepthAware<T> New<T>(IEnumerable<T> source)
{
return new DepthAware<T>(source);
}
}
Usage:
var chars = new[] {'a', 'b', 'c', 'd', 'e', 'f', 'g'}.AsDepthAware();
foreach (var item in chars)
{
Console.WriteLine("Char: {0}, depth: {1}", item, chars.Depth);
}
-
2Resurrecting a zombie since I just needed this: this will behave confusingly if the enumerable is iterated more than once. Jun 20, 2014 at 14:51
-
You are right. If I were to implement this today, I'd definitely go with the accepted answer.– Pz.Nov 4, 2019 at 11:51
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 kvp in myDictionary) {...}
foreach
that you can process each element absolute decoupled from its position in the list?