コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【C#】シーケンスから null の要素を除外する拡張メソッド

ソースコード

using System.Collections.Generic;
using System.Linq;

public static class IEnumerableExt
{
    public static IEnumerable<T> NotNull<T>( this IEnumerable<T> self )
    {
        return self.Where( c => c != null );
    }
}

使い方

var list = new [] { "フシギダネ", null, "ゼニガメ" };

var result1 = list.Where( c => c != null );

var result2 = list.NotNull();