Skip to content

Commit

Permalink
feat: Add some List<T> extension methods to make working with last el…
Browse files Browse the repository at this point in the history
…ements easier
  • Loading branch information
Vatsal Ambastha committed Mar 12, 2020
1 parent 0f1ecbd commit e31cd24
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,20 @@ public static bool IsNullOrEmpty<T>(this List<T> list) {
public static bool IsNullOrEmpty<T>(this T[] array) {
return array == null || array.Length == 0;
}
}

public static T FromLast<T>(this List<T> list, int index) {
if (index >= list.Count)
throw new IndexOutOfRangeException(index + " is out of range");
return list[list.Count - 1 - index];
}

public static T Last<T>(this List<T> list) {
return list.FromLast(0);
}

public static void RemoveLast<T>(this List<T> list) {
var last = list.Last();
list.Remove(last);
}
}
}

0 comments on commit e31cd24

Please sign in to comment.