Get all elements but the first from an array

C#Linq

C# Problem Overview


Is there a one-line easy linq expression to just get everything from a simple array except the first element?

for (int i = 1; i <= contents.Length - 1; i++)
	Message += contents[i];

I just wanted to see if it was easier to condense.

C# Solutions


Solution 1 - C#

Yes, Enumerable.Skip does what you want:

contents.Skip(1)

However, the result is an IEnumerable<T>, if you want to get an array use:

contents.Skip(1).ToArray()

Solution 2 - C#

The following would be equivalent to your for loop:

foreach (var item in contents.Skip(1))
    Message += item;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCielView Question on Stackoverflow
Solution 1 - C#LBushkinView Answer on Stackoverflow
Solution 2 - C#Dan StevensView Answer on Stackoverflow