EN
C# / .NET - min value in an array
0 points
This article will show you how to find the minimum value in an array in C# / .NET.
Quick solution:
xxxxxxxxxx
1
int[] array = new int[] { 1, 5, 2, 16, -5 };
2
3
int min = array.Min(); // from System.Linq
4
Console.WriteLine( min ); // -5
xxxxxxxxxx
1
using System;
2
using System.Linq;
3
4
public class Program
5
{
6
public static void Main(string[] args)
7
{
8
int[] array = new int[] { 1, 5, 2, 16, -5 };
9
10
int min = array.Min();
11
Console.WriteLine(min); // -5
12
}
13
}
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double Min(double[] array)
6
{
7
double result = Double.PositiveInfinity;
8
foreach (double currentNumber in array)
9
{
10
if (result > currentNumber) result = currentNumber;
11
}
12
return result;
13
}
14
15
public static void Main(string[] args)
16
{
17
double[] numbersArray = new double[] { 1.4, 5.0, 2.2, 16.0, -5.0 };
18
19
Console.WriteLine(Min(numbersArray)); // -5
20
}
21
}