Returns true if the enumerable contains a values in sequential order, or is empty. Returns false otherwise. - CSharp System

CSharp examples for System:Enum

Description

Returns true if the enumerable contains a values in sequential order, or is empty. Returns false otherwise.

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from w  w  w. j  a  va  2  s. c om

public class Main{
        /// <summary>
        /// Returns true if the enumerable contains a values in sequential order, or is empty. Returns false otherwise.
        /// </summary>
        public static bool IsSequential(this IEnumerable<int> enumeration)
        {
            var gotFirst = false;
            int lastValue = 0;

            foreach (var i in enumeration)
            {
                if (!gotFirst)
                {
                    lastValue = i;
                    gotFirst = true;
                    continue;
                }

                if (i != lastValue + 1)
                    return false;

                lastValue = i;
            }

            if (!gotFirst)
                return false;

            return true;
        }
}

Related Tutorials