In C#, Math.Clamp(value, min, max) makes sure a number stays inside a range. If the value is below min, you get min. If it is above max, you get max. Otherwise you get the value itself.
Think of a volume knob that goes from 0 to 100. If you turn the dial to 150, the speaker still plays at 100. Clamp does that job in a single call. Before it existed, you wrote Math.Min(Math.Max(value, min), max), which is harder to read (see Min() and Max()).
This is a static member of the static class System.Math. You call it with the class name, such as Math.Clamp(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Math.Clamp was added in .NET Core 2.0 and .NET Standard 2.1, so it is in modern .NET but not in classic .NET Framework.
Syntax
Pass the value, then the lowest and highest allowed numbers. All three should have the same type.
C#
int a = Math.Clamp(150, 0, 100); // 100
int b = Math.Clamp(-20, 0, 100); // 0
int c = Math.Clamp(55, 0, 100); // 55
Call
What it does
Math.Clamp(150, 0, 100)
Above the range, so it returns the maximum, 100.
Math.Clamp(-20, 0, 100)
Below the range, so it returns the minimum, 0.
Math.Clamp(55, 0, 100)
Inside the range, so it returns 55 unchanged.
Math.Clamp(5, 10, 0)
Throws ArgumentException because min (10) is greater than max (0).
The range is inclusive: a value equal to min or max is returned as is. min and max may be equal, which forces the answer to be that one number. For double, a NaN value gives NaN back. Overloads exist for int, long, double, decimal, and more.
Library declarations
C#
public static int Clamp(int value, int min, int max);
public static long Clamp(long value, long min, long max);
public static double Clamp(double value, double min, double max);
public static decimal Clamp(decimal value, decimal min, decimal max);
// also byte, sbyte, short, ushort, uint, ulong, float, nint, nuint overloads
Parameters
Parameter
Description
value
Required. The number to limit.
min
Required. The lowest number allowed. If value is smaller, min is returned.
max
Required. The highest number allowed. If value is bigger, max is returned. It must not be smaller than min.
Return Value
Return Value
Description
Same type as the inputs
min if value < min, max if value > max, otherwise value.
C# Examples
Example 1 — Below, Inside, and Above the Range
Clamp numbers into the range 0 to 100, and one double into 0 to 1.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.Clamp(150, 0, 100));
Console.WriteLine(Math.Clamp(-20, 0, 100));
Console.WriteLine(Math.Clamp(55, 0, 100));
Console.WriteLine(Math.Clamp(0, 0, 100));
Console.WriteLine(Math.Clamp(1.7, 0.0, 1.0));
}
}
Output
100
0
55
0
1
How It Works
Too high. 150 is above the maximum of 100, so the answer is 100.
Too low. -20 is below the minimum of 0, so the answer is 0.
Already fine. 55 is inside the range and comes back as 55. A value equal to min, like the 0, is also fine.
Doubles.1.7 is above the upper limit 1.0, so the double overload returns 1, printed as 1. This is how a colour or opacity value is kept between 0 and 1.
Example 2 — A Health Bar That Stays 0 to 100
Take damage in a loop and then heal. Clamp stops the health from going below 0 or above 100.
C#
using System;
class Program {
static void Main() {
int health = 100;
int[] hits = { 30, 30, 30, 30 };
foreach (int hit in hits) {
health = Math.Clamp(health - hit, 0, 100);
Console.WriteLine(health);
}
health = Math.Clamp(health + 60, 0, 100);
Console.WriteLine(health);
}
}
Output
70
40
10
0
60
How It Works
A starting value. Health starts at 100. Each hit removes 30, and the result is clamped to the range 0 to 100.
Normal hits. 100 - 30 is 70, then 40, then 10. All of these are inside the range, so nothing is changed.
The last hit. 10 - 30 would be -20. Clamp raises it to 0, so health never goes negative.
Healing is capped too. 0 + 60 is 60, which is inside the range. A heal of 150 would be cut at 100.
Example 3 — When Min Is Larger Than Max
See the exception for a reversed range, and how NaN passes through Clamp unchanged.
C#
using System;
class Program {
static void Main() {
int low = 10;
int high = 0;
try {
Console.WriteLine(Math.Clamp(5, low, high));
} catch (ArgumentException) {
Console.WriteLine("min is greater than max");
}
Console.WriteLine(Math.Clamp(double.NaN, 0, 10));
Console.WriteLine(Math.Clamp((byte)200, (byte)10, (byte)100));
}
}
Output
min is greater than max
NaN
100
How It Works
A reversed range. The lowest allowed number is 10 but the highest is 0. That makes no sense, so Math.Clamp throws ArgumentException.
Catch it. The catch block prints a message. In real code, fix the order of the two limits instead.
NaN passes through. For a double, NaN is not below or above anything, so Clamp returns NaN. Test with double.IsNaN first when it matters.
Other types. The (byte) casts select the byte overload. 200 is above 100, so the answer is 100.
Frequently Asked Questions
It limits a number to a range. If the value is below the minimum you get the minimum, if it is above the maximum you get the maximum, otherwise you get the value unchanged.
It was added in .NET Core 2.0 and .NET Standard 2.1, so it is available in modern .NET. It is not in classic .NET Framework, where you can write Math.Min(Math.Max(value, min), max) instead.
It throws ArgumentException. Make sure the lower limit comes before the upper limit in the argument list.
The order is Math.Clamp(value, min, max). The value you want to limit comes first. A frequent bug is writing Math.Clamp(0, 100, value), which has the min and max in the wrong place.
Yes, in effect. Math.Clamp(x, lo, hi) gives the same answer as Math.Min(Math.Max(x, lo), hi) for valid ranges, but is shorter and easier to read. It also throws for a reversed range instead of returning a surprising value.