What is the simplest way to get the tail of an array in C# - ie. all but the first element.
var myArray = GetSomeArray();
myArray = myArray.Skip(1).ToArray();
Note that I would avoid calling .ToArray() as much as possible and stick to using IEnumerable<T>
instead in .Net.
Array.Copy:
var array1 = new int[] { 1, 2, 3, 4, 5, 6 };
var array2 = new int[array1.Length - 1];
Array.Copy(array1, 1, array2, 0, array2.Length);
// array2 now contains {2, 3, 4, 5, 6}
Edit: Joel Coehoorn's answer is better, because it means you can avoid using arrays altogether!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With