This post will discuss how to determine the minimum and the maximum value in a list with its index in C#.

1. Using Min() and Max() Method

The standard solution to get the minimum value in a sequence of values is using the Min() method. Similarly to get the maximum value, use the Max() method. The following example demonstrates the usage of the Min() and Max() methods to find the minimum and maximum value in a list of integers.

Download  Run Code

Output:

Maximum element 9
Minimum element -7

 
We can easily extend the above solution to print the indices of the minimum and the maximum values. To get the indices, change the code like this:

Download  Run Code

Output:

Maximum element 9 present at index 3
Minimum element -7 present at index 4

2. Using Enumerable.Select Method

The Enumerable.Select method projects each element of a sequence into a new form by incorporating the element’s index. The following example demonstrates the usage of the Select() method to find both value and index to find the minimum and the maximum element in a single line.

Download  Run Code

Output:

Maximum element 9 present at index 3
Minimum element -7 present at index 4

3. Using Custom Routine

Alternatively, we can write a custom routine to find the minimum and the maximum values in a list. The idea is to iterate through the list and keep track of the minimum and the maximum value so far (and optionally index). Finally, return the minimum and the maximum value. Here’s what the code would look like:

Download  Run Code

Output:

Maximum element 9
Minimum element -7

That’s all about finding the minimum and the maximum values in a list with its index in C#.