title: C# foreach loop with key value
body: <p>In PHP I can use a foreach loop such that I have access to both the key and value for example:</p>
  
  <pre><code>foreach($array as $key =&gt; $value)
  </code></pre>
  
  <p>I have the following code:</p>
  
  <pre><code>Regex regex = new Regex(pattern);
  MatchCollection mc = regex.Matches(haystack);
  for (int i = 0; i &lt; mc.Count; i++)
  {
       GroupCollection gc = mc[i].Groups;
       Dictionary&lt;string, string&gt; match = new Dictionary&lt;string, string&gt;();
       for (int j = 0; j &lt; gc.Count; j++)
       {
          //here
       }
       this.matches.Add(i, match);
  }
  </code></pre>
  
  <p>at <code>//here</code> I'd like to <code>match.add(key, value)</code> but I cannot figure out how to get the key from the GroupCollection, which in this case should be the name of the capturing group.  I know that <code>gc["goupName"].Value</code> contains the value of the match.</p>