Select all the items in this array from the beginning until (but not including) If is not found in the array, Until will select all items. If is the first item in the array, an empty array will be returned. - CSharp System

CSharp examples for System:Array Element

Description

Select all the items in this array from the beginning until (but not including) If is not found in the array, Until will select all items. If is the first item in the array, an empty array will be returned.

Demo Code

//     Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
using System.Linq;
using System.Collections.Generic;
using System;//from w w w  . j  a  v  a 2 s.co  m

public class Main{
        /// <summary>
        /// Select all the items in this array from the beginning until (but not including) <see cref="startingItem"/>
        /// 
        /// If <see cref="startingItem"/> is not found in the array, Until will select all items.
        /// If <see cref="startingItem"/> is the first item in the array, an empty array will be returned.
        /// </summary>
        internal static IEnumerable<T> Until<T>(this IEnumerable<T> items, T startingItem)
        {
            var enumerator = items.GetEnumerator();
            while (enumerator.MoveNext())
            {
                var current = enumerator.Current;
                if (Equals(current, startingItem))
                    yield break;
                yield return current;
            }
        }
}

Related Tutorials