c# list<int> how to insert a new value in between two values
List<int> initializers = new List <int>();
initializers.Add(1);
initializers.Add(3);
int index = initializers.IndexOf(3);
initializers.Insert(index, 2);
Gives you 1,2,3.
Use List<T>.Insert
:
initializers.Insert(index, value);
You can just use List.Insert() instead of List.Add() to insert items at a specific position.