ソースコード
using System; using System.Collections.Generic; public static class IListExtensions { /// <summary> /// <para>シーケンス内の指定されたインデックス位置にある要素を返します。</para> /// <para>インデックスが範囲外の場合は既定値を返します。</para> /// </summary> public static T ElementAtOrDefault<T>( this IList<T> self, int index, T defaultValue ) { return index >= 0 && index < self.Count ? self[ index ] : defaultValue; } }
使い方
var list = new []{ 1, 2, 3, 4, 5 }; list.ElementAtOrDefault( 10, -1 );