Thursday, September 8, 2016

Insert something at the very beginning of a list in C#.

List<string> strings = new List<string>() { "foo", "bar", "baz" };
strings.Insert(0, "qux");

 
 

The list will end up with this in it:

  1. qux
  2. foo
  3. bar
  4. baz

 
 

Addendum 10/12/2018: The zero doesn't have to be a zero. You may use this trick to insert midstream into a list.

No comments:

Post a Comment