/// <summary>
        /// Checks if a sequence is NOT empty.
        /// </summary>
        /// <param name="seq">The sequence to check.</param>
        /// <returns>Sequence is empty (<see langword="false" />) or not (<see langword="true" />).</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="seq" /> is <see langword="null" />.
        /// </exception>
        public static bool IsNotEmpty(IEnumerable seq)
        {
            if (seq == null)
            {
                throw new ArgumentNullException("seq");
            }

            IGeneralList genList = seq as IGeneralList;

            if (seq != null)
            {
                return(genList.IsNotEmpty);
            }

            return(IsEmpty(seq) == false);
        }
        /// <summary>
        /// Extended foreach operation for a sequence.
        /// </summary>
        /// <typeparam name="T">Type of the items.</typeparam>
        /// <typeparam name="S">Type of the state object.</typeparam>
        /// <param name="seq">The sequence.</param>
        /// <param name="action">The action to invoke.</param>
        /// <param name="actionStateFactory">
        /// The function that returns the state object for argument of <paramref name="action" />.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="seq" />, <paramref name="action" /> and/or
        /// <paramref name="actionStateFactory" /> are <see langword="null" />.
        /// </exception>
        public static void ForEach <T, S>(IEnumerable <T> seq, Action <IForEachItemExecutionContext <T, S> > action, Func <T, long, S> actionStateFactory)
        {
            if (seq == null)
            {
                throw new ArgumentNullException("seq");
            }

            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            if (actionStateFactory == null)
            {
                throw new ArgumentNullException("actionStateFactory");
            }

            IGeneralList genList = seq as IGeneralList;

            if (genList != null)
            {
                genList.ForEach <T, S>(action, actionStateFactory);
                return;
            }

            using (IEnumerator <T> e = seq.GetEnumerator())
            {
                long index = -1;

                while (e.MoveNext())
                {
                    SimpleForEachItemExecutionContext <T, S> ctx = new SimpleForEachItemExecutionContext <T, S>();
                    ctx.Cancel = false;
                    ctx.Index  = ++index;
                    ctx.Item   = e.Current;
                    ctx.State  = actionStateFactory(ctx.Item, ctx.Index);

                    action(ctx);

                    if (ctx.Cancel)
                    {
                        // cancel whole operation
                        break;
                    }
                }
            }
        }
Esempio n. 3
0
        // Public Methods (1) 

        /// <summary>
        /// Converts a general sequence to an object array.
        /// </summary>
        /// <param name="seq">The sequence to convert / cast.</param>
        /// <returns><paramref name="seq" /> as array.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="seq" /> is <see langword="null" />.</exception>
        public static object[] ToArray(IEnumerable seq)
        {
            if (seq == null)
            {
                throw new ArgumentNullException("seq");
            }

            IGeneralList genList = seq as IGeneralList;

            if (genList != null)
            {
                // use build-in
                return(genList.ToArray());
            }

            return(ToArray <object>(AsSequence <object>(seq)));
        }
Esempio n. 4
0
        // Public Methods (1) 

        /// <summary>
        /// Filters the items from a sequence of a specific result type.
        /// </summary>
        /// <typeparam name="T">Result type.</typeparam>
        /// <param name="seq">The input sequence.</param>
        /// <returns>The sequence with the filtered items.</returns>
        public static IEnumerable <T> OfType <T>(IEnumerable seq)
        {
            if (seq == null)
            {
                throw new ArgumentNullException("seq");
            }

            IGeneralList genList = seq as IGeneralList;

            if (genList != null)
            {
                // use build-in
                return(genList.OfType <T>());
            }

            return(OfTypeInner <T>(seq));
        }
Esempio n. 5
0
        /// <summary>
        /// Checks if a sequence is empty.
        /// </summary>
        /// <param name="seq">The sequence to check.</param>
        /// <returns>Sequence is empty or not.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="seq" /> is <see langword="null" />.
        /// </exception>
        public static bool IsEmpty(IEnumerable seq)
        {
            if (seq == null)
            {
                throw new ArgumentNullException("seq");
            }

            IGeneralList genList = seq as IGeneralList;

            if (genList != null)
            {
                // use build-in
                return(genList.IsEmpty);
            }

            return(Count(seq) < 1);
        }
        // Public Methods (1) 

        /// <summary>
        /// Casts the items of a general sequence to a specific target type.
        /// </summary>
        /// <typeparam name="T">Target type of the items.</typeparam>
        /// <param name="seq">The input sequence.</param>
        /// <returns>The sequence with the converted items.</returns>
        /// <remarks>
        /// If <paramref name="seq" /> is already the return type, it is simply casted.
        /// </remarks>
        public static IEnumerable <T> Cast <T>(IEnumerable seq)
        {
            if (seq == null)
            {
                throw new ArgumentNullException("seq");
            }

            IEnumerable <T> typedSeq = seq as IEnumerable <T>;

            if (typedSeq != null)
            {
                return(typedSeq);
            }

            IGeneralList genList = seq as IGeneralList;

            if (genList != null)
            {
                // use build-in
                return(genList.Cast <T>());
            }

            return(CastInner <T>(seq));
        }