以下の内容はhttps://baba-s.hatenablog.com/entry/2019/11/28/170000より取得しました。


【Unity】List.Find の null チェックを少しだけ簡潔に記述できる拡張メソッド

ソースコード

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 );
}



以上の内容はhttps://baba-s.hatenablog.com/entry/2019/11/28/170000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14