How would one create a method that takes an integer i
, and move the member of a List<T>
at index i
from its current position to the front of the list?
The List<T> class doesn't offer such a method, but you can write an extension method that gets the item, removes it and finally re-inserts it:
static class ListExtensions
{
static void MoveItemAtIndexToFront<T>(this List<T> list, int index)
{
T item = list[index];
list.RemoveAt(index);
list.Insert(0, item);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With