Is there a C# equivalent of Python's enumerate() and Ruby's each_with_index?
If you're using LINQ, there are overrides of the various functions that allow for enumeration. Otherwise, you're usually stuck using a variable that you increment yourself.
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
}
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) {...}
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 collect. It will have two properties
- Value: with the original value in the collection
- Index: with the index within the collection
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.
If you're using a collection you could use the indexOf() function inside of the foreach loop:
foreach(FName name in NameList)
{
return NameList.indexOf(name);
}
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));
}
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.
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
} );
Here's an extension method should do what your needing: http://gist.github.com/246724