List<T>.FindやList<T>.FindIndex
の使い方はこんな感じ。
using System;
using System.Collections.Generic;
public class Example
{
static public void Main ()
{
List<int> example = new List<int>{ 3, 1, 4, 1, 5 };
Console.WriteLine("Find: " + example.Find((e) => { return e % 2 == 0; }));
Console.WriteLine("FindIndex: " + example.FindIndex((e) => { return e % 2 == 0; }));
}
}
実行結果
Find: 4
FindIndex: 2
気になるポイントは一致するものがなかった場合の戻り値。
Find: 型Tの既定値
FindIndex: -1
よく忘れるのでメモしておこうかなと思います。
using System;
using System.Collections.Generic;
public class Example
{
static public void Main ()
{
List<int> example = new List<int>{ 3, 1, 4, 1, 5, 9, 2 };
Console.WriteLine("Find: " + example.Find((e) => { return e == 6; }));
Console.WriteLine("FindIndex: " + example.FindIndex((e) => { return e == 6; }));
}
}
実行結果
Find: 0
FindIndex: -1
リンク
List(T).Find メソッド (Predicate(T)) (System.Collections.Generic)
https://msdn.microsoft.com/ja-jp/library/x0b5b5bc(v=vs.110).aspx
List(T).FindIndex メソッド (Int32, Int32, Predicate(T)) (System.Collections.Generic)
https://msdn.microsoft.com/ja-jp/library/7eb596wz(v=vs.110).aspxタグ:C#
2018年05月15日
C#のList<T> Find/List<T> FindIndexについて
posted by unity-chan at 21:55
| Script