So I have a generic list, and an
oldIndex and a
I want to move the item at
newIndex...as simply as possible.
The item should be end up between the items at
(newIndex - 1) and
newIndex before it was removed.
I know you said "generic list" but you didn't specify that you needed to use the List(T) class so here is a shot at something different.
The ObservableCollection(T) class has a Move method that does exactly what you want.
public void Move(int oldIndex, int newIndex)
Underneath it is basically implemented like this.
T item = base[oldIndex]; base.RemoveItem(oldIndex); base.InsertItem(newIndex, item);
So as you can see the swap method that others have suggested is essentially what the ObservableCollection does in it's own Move method.
var item = list[oldIndex]; list.RemoveAt(oldIndex); if (newIndex > oldIndex) newIndex--; // the actual index could have shifted due to the removal list.Insert(newIndex, item);