fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var list = Enumerable.Range(0, 10).ToList();
  10. Console.WriteLine("- move 5 to 1 -");
  11. list.MoveItem(5, 1);
  12. list.ForEach(n => Console.WriteLine(n));
  13.  
  14. Console.WriteLine("- move 2 to 9 -");
  15. list.MoveItem(2, 9);
  16. list.ForEach(n => Console.WriteLine(n));
  17. }
  18. }
  19.  
  20. static class Extensions
  21. {
  22. public static void MoveItem<T>(this List<T> list, int from, int to)
  23. {
  24. var target = list[from];
  25. list.RemoveAt(from);
  26. list.Insert(to, target);
  27. }
  28. }
  29.  
Success #stdin #stdout 0.02s 17068KB
stdin
Standard input is empty
stdout
- move 5 to 1 -
0
5
1
2
3
4
6
7
8
9
- move 2 to 9 -
0
5
2
3
4
6
7
8
9
1