Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the tail of an array

Tags:

c#

What is the simplest way to get the tail of an array in C# - ie. all but the first element.

like image 404
Joel Avatar asked May 17 '10 03:05

Joel


2 Answers

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.

like image 66
Joel Coehoorn Avatar answered Sep 21 '22 15:09

Joel Coehoorn


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!

like image 24
Dean Harding Avatar answered Sep 23 '22 15:09

Dean Harding