Obtain the index of the maximum element

前端 未结 9 1526
梦谈多话
梦谈多话 2020-12-28 15:03

Given such a list:

        List intList = new List();
        intList.Add(5);
        intList.Add(10);
        intList.Add(15);
                


        
9条回答
  •  隐瞒了意图╮
    2020-12-28 15:50

    public static class Extensions
    {
        public static int MaxIndex(this IEnumerable TSource)
        {
            int i = -1;
            using (var iterator = TSource.GetEnumerator())
                while (iterator.MoveNext())
                    i++;
            return i;
        }
    }
    

    Here is my crack at this problem. I returned -1 instead of throwing an exception because this is what the FindIndex function does and I find it very convenient.

提交回复
热议问题