ソースコード
using System; using System.Collections.Generic; public static class ListExt { public static bool TryFind<T>( this List<T> self, Predicate<T> match, out T result ) where T : class { result = self.Find( match ); return result != null; } }
使用例
通常
var result = list.Find( c => c.Contains( "ピカチュウ" ) ); if ( result != null ) { Console.WriteLine( result ); }
拡張メソッド
if ( list.TryFind( c => c.Contains( "ピカチュウ" ), out var result ) ) { Console.WriteLine( result ); }