Check if List values are consecutive

前端 未结 12 1829
悲&欢浪女
悲&欢浪女 2021-02-01 05:46
List dansConList = new List();
dansConList[0] = 1;
dansConList[1] = 2;
dansConList[2] = 3;

List dansRandomList = new List

        
12条回答
  •  悲哀的现实
    2021-02-01 06:18

    Extension method:

    public static bool IsConsecutive(this IEnumerable myList)
    {
        return myList.SequenceEqual(Enumerable.Range(myList.First(), myList.Last()));
    }
    

    Useage:

    bool isConsecutive = dansRandomList.IsConsecutive();
    

提交回复
热议问题