Skip to content

Instantly share code, notes, and snippets.

@tobiashm
Created December 3, 2010 12:09
Show Gist options
  • Save tobiashm/726891 to your computer and use it in GitHub Desktop.
Save tobiashm/726891 to your computer and use it in GitHub Desktop.
Extension methods for .NET
// Source: http://www.pluralsight-training.net/community/blogs/keith/archive/2010/11/12/min-max-between-limit.aspx
public static class IComparableExtensions
{
public static T Limit<T>(this T value, T minValue, T maxValue) where T : IComparable
{
if (value.CompareTo(minValue) < 0)
return minValue;
if (value.CompareTo(maxValue) > 0)
return maxValue;
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment