This operation is called ‘Clamp’ and it’s usually written like this:
public static int Clamp( int value, int min, int max )
{
return (value < min) ? min : (value > max) ? max : value;
}
More Related Contents:
- Fastest implementation of log2(int) and log2(float)
- Best way to reverse a string
- String vs. StringBuilder
- How slow are .NET exceptions?
- Is double Multiplication Broken in .NET? [duplicate]
- How to get object size in memory? [duplicate]
- Benchmarking small code samples in C#, can this implementation be improved?
- Does C# optimize the concatenation of string literals?
- Is String.Contains() faster than String.IndexOf()?
- Algorithm for simplifying decimal to fractions
- LINQ: Not Any vs All Don’t
- What is the real overhead of try/catch in C#?
- String interning in .Net Framework – What are the benefits and when to use interning
- Why is LINQ .Where(predicate).First() faster than .First(predicate)?
- C# / F# Performance comparison
- SQLite .NET performance, how to speed up things?
- How does extern work in C#?
- How performant is StackFrame?
- Performance of Find() vs. FirstOrDefault() [duplicate]
- Does String.GetHashCode consider the full string or only part of it?
- How do I measure how long a function is running?
- Warm-up when calling methods in C#
- LINQ performance FAQ
- Is it better to execute many sql commands with one connection, or reconnect every time?
- Why is my C# program faster in a profiler?
- Array bounds check efficiency in .net 4 and above
- Does any one know of a faster method to do String.Split()?
- Heap versus Stack allocation implications (.NET)
- Why does Math.Exp give different results between 32-bit and 64-bit, with same input, same hardware
- How to round a integer to the close hundred?