どうも、ちょげ(@chogetarou)です。
List(リスト)の最大値を取得する方法を紹介します。
方法
Listの最大値を取得するには、Maxを使います。
まず、System.Linqを取り入れます。
using System.Linq;
そして、リストからMaxメソッドを呼び出します。
list.Max();
Maxメソッドは、呼び出したListの最大値を返します。
使用例
using System;
using System.Linq;
using System.Collections.Generic;
public class Sample{
public static void Main(){
List<int> numbers = new List<int>() { 1, 1, 3, 10, 4, 5 };
int max = numbers.Max();
Console.WriteLine(max); //10
}
}
コメント