c sharp

What is Math.Min() Method in C#

In C# there are many mathematical functions and one of them is Math.Min(). This method is employed to tell the smaller value among two numeric parameters that are passed into it. This tutorial will make you familiar with Math.Min() in C# programming.

What is Math.Min() Method in C#

The Math.Min() is a static function in C# that produces the less significant value among the two supplied values. It takes two arguments of numeric data types like int, double, float, and furthermore. The method evaluates the two inputs and returns the lower value.

Syntax
The syntax for applying Math.Min() method is as follows:

Math.Min(var1, var2);

Here, in the above syntax, the Math.Min() method performs a comparison of two variables: var1 and var2. The technique returns the variable value less than the other variable. Now, will make use of this method in the implementation of C# examples.

1: Find a Minimum Integer Value with Math.Min() Method

Below is the basic example of C# code that detects the lesser of two integer numerals by using Math.Min() method:

using System;

class Program
{
    static void Main()
    {
        int num1 = 23;
        int num2 = 44;
        int smaller = Math.Min(num1, num2);
        Console.WriteLine("The smaller number between num1 and num2 is: " + smaller );
    }
}

First, we declared two integer variables, num1, and num2, and allocate them the values 23 and 44, correspondingly. The method Math.Min() is then used to find the smallest of the two integers, and assigned to a new numeric variable smaller. Finally, we use the Console.WriteLine() method to write a result of smaller to the console. When we execute this program, we will get the following output:

2: Find a Minimum Float, Decimal, and Short Value with Math.Min() Method

The following example implements, Math.Min() function to find minimum value concerning different data types variables such as float, decimal, and short:

using System;
public class Demo {
   public static void Main(){
      float num1 = 12.8f, num2 = 25.6f;
      decimal num3 = 100M, num4 = 10M;
      short num5 = 20, num6 = 30;
      Console.WriteLine("Minimum-Value from two float values = "+Math.Min(num1, num2));
      Console.WriteLine("Minimum-Value from two double values = "+Math.Min(num3, num4));
      Console.WriteLine("Minimum-Value from two short values = "+Math.Min(num5, num6));
   }
}

First, in the main function, we initialized two variables of each data type. In the end, we simply showed the smallest value among the respective data types’ numbers using Math.Min(), and print them on the screen using Console.WriteLine() in C# as the output below:

Math.Min() Method Limitations

In C#, the Math.Min() method can only compare two values. If you wish to determine the minimum value in a group of values, you must use an alternative approach. The Min() method of System.Linq can identify the minimum value in a collection.

Conclusion

In C#, the Math.Min() function is useful for determining the minimum of two values. However, it can only compare two values; if you wish to identify the lowest value in a set of values, you must use another method.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.