Archive for category Linq

How to check if an array of elements are in Sequential Order in C#

You can use the SequenceEqual method to check if numbers are in Sequence order.  SequenceEqual method determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.


//Your Input
int[] numSequence = { 1, 2, 3, 4, 5 };
//Check if they are in sequence or not
bool isInSequence = numSequence.SequenceEqual(Enumerable.Range(1, numSequence.Count()));

Above code will give the result as true if numbers are in proper sequence.

You also need to ensure that you have added the below namespace for Linq in your page

using System.Linq;

, ,

Leave a comment