如何foreach在C#循环中获取当前元素的键?
例如:
foreach ($array as $key => $value)
{
    echo("$value is assigned to key: $key");
}
int[] values = { 5, 14, 29, 49, 99, 150, 999 };
foreach (int val in values)
{
    if(search <= val && !stop)
    {
         // Set key to a variable
    }
}
说我有
List<int> ages  = new List<int>() { 8, 5, 3, 9, 2, 1, 7 };
List<int> marks = new List<int>() { 12, 17, 08, 15, 19, 02, 11 };
我可以在我的排序,marks通过ages这样的:
while (true)
{
  bool swapped = false;
  for (int i = 0; i < ages.Count - 1; i++)
    if (ages[i] > ages[i + 1])
    {
      int tmp = ages[i];
      ages[i] = ages[i + 1];
      ages[i + 1] = tmp;
      tmp = marks[i];
      marks[i] = marks[i + …