List dansConList = new List();
dansConList[0] = 1;
dansConList[1] = 2;
dansConList[2] = 3;
List dansRandomList = new List
Here is an extension method that uses the Aggregate function.
public static bool IsConsecutive(this List value){
return value.OrderByDescending(c => c)
.Select(c => c.ToString())
.Aggregate((current, item) =>
(item.ToInt() - current.ToInt() == -1) ? item : ""
)
.Any();
}
Usage:
var consecutive = new List(){1,2,3,4}.IsConsecutive(); //true
var unorderedConsecutive = new List(){1,4,3,2}.IsConsecutive(); //true
var notConsecutive = new List(){1,5,3,4}.IsConsecutive(); //false