Adding Items To The Beginning Of A Collection in C# & .NET
[C#, .NET]
A scenario you have probably run into is needing to modify a collection by inserting items at the beginning.
Take, for instance, the following List of integers.
var numbers = new List<int>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
If we wanted to insert a 0 at the start of this list, there are a number of ways to do this:
Insert
The first is to use the Insert method, which takes the following parameters:
- The position to insert
- The value to insert
Like so:
List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
numbers.Insert(0, 0);
Prepend
Another way is to use the Prepend method:
List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
numbers.Prepend(0);
Collection Expressions
Another way is to use collection expressions as follows:
List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
List<int> newList = [0, .. numbers];
Unlike the previous two methods, this does not mutate the existing collection - it creates a new one.
TLDR
There are several ways to add items to the beginning of a collection.
Happy hacking!