How do I get the key of the current element in a foreach loop in C#?
For example:
foreach ($array as $key => $value)
{
If you want to get at the key (read: index) then you'd have to use a for loop. If you actually want to have a collection that holds keys/values then I'd consider using a HashTable or a Dictionary (if you want to use Generics).
Dictionary items = new Dictionary();
foreach (int key in items.Keys)
{
Console.WriteLine("Key: {0} has value: {1}", key, items[key]);
}
Hope that helps, Tyler