Help us understand the problem. What is going on with this article?

キーと値が連続で並ぶ配列から、キー、値のペアを列挙したい

More than 5 years have passed since last update.

キーと値が連続で並ぶ配列から、キー、値のペアを列挙したいとき、下記のように書いた。
他に良い方法があるかも。

pair.cs
var hoges = new string[] { "key1", "value1", "key2", "value2", "key3", "value", };

var numOfPair = hoges.Count() / 2;
foreach (var pair in Enumerable.Range(0, numOfPair)
                               .Select(i => new { Key   = hoges[i * 2], 
                                                  Value = hoges[i * 2 + 1], }))
{
    // pair.Key ...
    // pair.Value ...
}
SheepCloud
C#でもっと遊びたいが、おウチにパソコンが無い。 だれかパソコン下さい。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away