I wanted to find consecutive and non-consecutive numbers separated by hyphens. Once i ran this given code it gave me an error as "System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'". What is wrong with this code, please explain if anyone knows..
public static void Main(string[] args)
{
Console.Write("Enter a few numbers (eg 1-2-3-4): ");
var input = Console.ReadLine();
var numArray = new List<int>();
foreach (var number in input.Split('-'))
{
numArray.Add(Convert.ToInt32(number));
}
numArray.Sort();
var isConsecutive = true;
for (int i = 0; i < numArray.Count; i++)
{
if (numArray[i] != numArray[i + 1] - 1)
{
isConsecutive = false;
break;
}
}
if (isConsecutive)
{
Console.WriteLine("Consecutive");
Console.ReadLine();
}
else
{
Console.WriteLine("Not Consecutive");
Console.ReadLine();
}
}