EN
C# / .NET - max value in an array
0 points
This article will show you how to find the maximum value in an array in C# / .NET.
Quick solution:
xxxxxxxxxx
1
int[] array = new int[] { 1, 5, 2, 16, -5 };
2
3
int max = array.Max(); // from System.Linq
4
Console.WriteLine( max ); // 16
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 max = array.Max();
11
Console.WriteLine( max ); // 16
12
}
13
}
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double Max(double[] array)
6
{
7
double result = Double.NegativeInfinity;
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( Max( numbersArray ) ); // 16
20
}
21
}