简体   繁体   中英

Get all elements but the first from an array

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.

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()

The following would be equivalent to your for loop:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM