Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 283 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
foreach with index

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

#2
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) {...}
Reply

#3
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]

Reply

#4
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]));
Reply

#5
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" }))
Reply

#6
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]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through