Skip to content

Instantly share code, notes, and snippets.

@pietrom
Created September 8, 2016 14:40
Show Gist options
  • Save pietrom/edb627cd487a00683ffb4f72f268b990 to your computer and use it in GitHub Desktop.
Save pietrom/edb627cd487a00683ffb4f72f268b990 to your computer and use it in GitHub Desktop.
public static class ObjectHelper {
public static T? Min<T>(T? x, T? y) where T : struct, IComparable {
if (x.HasValue && y.HasValue) {
return x.Value.CompareTo(y.Value) <= 0 ? x : y;
} else if (x.HasValue) {
return x;
} else {
return y;
}
}
public static T? MinWith<T>(this T? x, T? y) where T : struct, IComparable {
return Min(x, y);
}
public static T? Max<T>(T? x, T? y) where T : struct, IComparable {
if (x.HasValue && y.HasValue) {
return x.Value.CompareTo(y.Value) >= 0 ? x : y;
} else if (x.HasValue) {
return x;
} else {
return y;
}
}
public static T? MaxWith<T>(this T? x, T? y) where T : struct, IComparable {
return Max(x, y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment