How would you get the index of the lowest value in an int array?

前端 未结 5 1806
旧巷少年郎
旧巷少年郎 2020-12-28 16:45

Considering that this is a very basic task, I could not think of an appropriately easy way to do it. How would you get the index of the lowest value in an int array? Using L

5条回答
  •  再見小時候
    2020-12-28 17:21

    int[] data = new []{ 10, 2, 3, 4, 2 };
    var index = data
      .Select((v, i) =>new {Index = i, Val = v})
      .FirstOrDefault(v => v.Val == data.Min()).Index; // 1
    

    But be careful, because you remember that you will get an exception if the array is null

提交回复
热议问题