Returns all the elements but the last - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Returns all the elements but the last

Demo Code


using System.Linq;
using System.Collections.Generic;

public class Main{
        /// <summary>
        /// Returns all the elements but the last
        /// </summary>
        /// <typeparam name="T">Type of the collection</typeparam>
        /// <param name="sequence">collection to use</param>
        /// <returns>result = collection@pre->at( 0, length - 2 )</returns>
        public static IEnumerable<T> Head<T>(this IEnumerable<T> sequence)
        {/*  w  ww.j a  v a  2 s . co  m*/
            return sequence.Take(sequence.Count() - 1);
        }
}

Related Tutorials