Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tateisu/1af488861ea0272f7111e2e7736b9c0b to your computer and use it in GitHub Desktop.
Save tateisu/1af488861ea0272f7111e2e7736b9c0b to your computer and use it in GitHub Desktop.
ジェネリックメソッドの戻り値を T? と書けないC#8.0かっこ悪い
#nullable disable
public static T elementOrNull<T>(this List<T> list, Int32 index) {
try {
return list[ index ];
} catch (IndexOutOfRangeException) {
return default;
}
}
public static T firstOrNull<T>(this List<T> list) => elementOrNull( list, 0 );
public static T lastOrNull<T>(this List<T> list) => elementOrNull( list, list.Count-1 );
#nullable enable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment