LoginSignup
4

More than 5 years have passed since last update.

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

Posted at

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

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 ...
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4