Created
May 5, 2020 07:00
-
-
Save tateisu/1af488861ea0272f7111e2e7736b9c0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ジェネリックメソッドの戻り値を 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