HomeSearch | ## C# Math.Max and Min, Bounds CheckThis C# article shows the Math.Max method. It rewrites if-statements. | |

## Math.Maxreturns the larger value. It can improve the style of code that calculates integer bounds. Often in algorithms we need to test if an integer is less than zero. We use Math.Max to test for these conditions with less code.
| ||

## Example.This example uses the Math.Max method to check bounds. You can replace many if-statements with one line of code using Math.Max. We take the maximum of zero and the value. The result will never be less than zero.
| C# program that uses Math
using System;
class Program
{
static int[] _array = new int[]
{
1,
2,
5,
6,
7
};
static void Main()
{
// Get 1 place from end.
int i = GetLastIndex(1);
Console.WriteLine(i);
// Get 3 places from end.
i = GetLastIndex(3);
Console.WriteLine(i);
// Get 10 places from end. Will not throw exception.
i = GetLastIndex(10);
Console.WriteLine(i);
}
static int GetLastIndex(int length)
{
int start = _array.Length - length - 1;
start = Math.Max(0, start);
return _array[start];
}
}
Output
6
2
1 | |

## Discussion.You will need to test again zero many times in your career as a developer. This is because using a negative number on the indexer will throw an exception. What you will get is an IndexOutOfRangeException.IndexOutOfRangeException | ||

## Also,I have benchmarked Math.Min and Math.Max and they are no different from typing the nine lines yourself. The only difference is that your code is shorter, simpler to read, and less prone to typos. | ||

## Example 2.Here we see another example of how you can rewrite if-statements to code that uses the Math class. This is important because with it you can impress your interviewers. It just makes your code more maintainable. | Code example that uses if: C#
using System;
class Program
{
static void Main()
{
// Constrain indexes with if-statement.
int a = 4;
if (a < 0)
{
Console.WriteLine(0);
}
else
{
Console.WriteLine(a);
}
}
}
Code example that uses Math.Max: C#
using System;
class Program
{
static void Main()
{
// Constrain indexes with Math.Max.
int a = 4;
Console.WriteLine(Math.Max(0, a));
}
} | |

## Math.Min.Next, we see how to rewrite an if-condition expression to use the method Math.Min on integral types. This example caps values at a certain number using Math.Min. This code is efficient and often useful.Math.Min
| Code that uses if: C#
using System;
class Program
{
static void Main()
{
// Make sure it isn't > 10.
int b = 15;
if (b > 10)
{
b = 10;
}
Console.WriteLine(b);
}
}
Code that uses Math.Min: C#
using System;
class Program
{
static void Main()
{
// Make sure it isn't > 10.
int b = 15;
Console.WriteLine(Math.Min(10, b));
}
} | |

## Summary.We clarified bounds-checking, simplified it, and impressed interviewers with Math.Min and Math.Max. There are many other methods on the Math static class, most of which can have similar effects on code quality.Static Class | ||

Home Dot Net Perls | © 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com. |