どうも、ちょげ(@chogetarou)です。
Dictionary(辞書)のValue(値)の最小値を取得する方法を紹介します。
方法

Dictionary(辞書)のValue(値)の最小値を取得するには、Min()を使います。
まず、System.Linqを導入します。
using System.Linq;
DictionaryからValuesプロパティにアクセスします。
そして、Valueプロパティから Min()を呼び出します。
T minValue = myDict.values.Min();
上記のMin()は、Dictionary(辞書)のValue(値)の最小値を返します。
使用例
using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
public static void Main()
{
Dictionary<string, int> numbers = new Dictionary<string, int>()
{
{ "one", 1 },
{ "two", 2 },
{ "three", 3 },
{ "ten", -10 },
{ "four", 4 },
{ "five", 5 },
};
int minValue = numbers.Values.Min();
Console.WriteLine(minValue); //-10
}
}
コメント