Languages
[Edit]
EN

C# / .NET - min value in an array

0 points
Created by:
Trenton-McKinney
318

This article will show you how to find the minimum value in an array in C# / .NET.

Quick solution:

int[] array = new int[] { 1, 5, 2, 16, -5 };

int min = array.Min(); // from System.Linq
Console.WriteLine( min ); // -5

 

Practical examples

1. With Min() method from System.Linq.

using System;
using System.Linq;

public class Program
{
    public static void Main(string[] args)
    {
        int[] array = new int[] { 1, 5, 2, 16, -5 };

        int min = array.Min();
        Console.WriteLine(min); // -5
    }
}

2. By comparing all the elements in the array

using System;

public class Program
{
    static double Min(double[] array)
    {
        double result = Double.PositiveInfinity;
        foreach (double currentNumber in array)
        {
            if (result > currentNumber) result = currentNumber;
        }
        return result;
    }

    public static void Main(string[] args)
    {
        double[] numbersArray = new double[] { 1.4, 5.0, 2.2, 16.0, -5.0 };

        Console.WriteLine(Min(numbersArray)); // -5
    }
}

See also

  1. C# / .NET - Math.Min() method example

  2. Maxima and minima - Wikipedia

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join