どうも、ちょげ(@chogetarou)です。
foreachでDictionary(連想配列)の値(Value)をループする方法を紹介します。
方法

foreachでDictionary(連想配列)の値(value)をループするには、Valuesプロパティを使います。
具体的には、foreachのループ対象に、DictionaryのValuesプロパティを指定します。
foreach (var value in dict.Values)
{
//ループ処理
}
ループ対象にDictionaryのValuesプロパティを指定することで、foreachでDictioanry(連想配列)の値(Value)をループできます。
使用例
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
Dictionary<string, int> numbers = new Dictionary<string, int>()
{
{ "one", 1 },
{ "two", 2 },
{ "three", 3 },
{ "four", 4 },
{ "five", 5 },
};
foreach (var value in numbers.Values)
{
Console.WriteLine(value);
}
}
}

コメント