我如何用c#优雅地做到这一点?
例如,一个数字可以是1到100之间。
我知道一个简单的if (x >= 1 && x <= 100)就足够了;但是有很多语法糖和新特性不断添加到c# /。Net这个问题是关于更习惯的(一个可以称之为优雅的)写法。
性能不是问题,但请在非O(1)的解决方案中添加性能说明,因为人们可能会复制粘贴建议。
我如何用c#优雅地做到这一点?
例如,一个数字可以是1到100之间。
我知道一个简单的if (x >= 1 && x <= 100)就足够了;但是有很多语法糖和新特性不断添加到c# /。Net这个问题是关于更习惯的(一个可以称之为优雅的)写法。
性能不是问题,但请在非O(1)的解决方案中添加性能说明,因为人们可能会复制粘贴建议。
当前回答
好吧,我会配合的。已经有这么多答案了,但也许还有一些其他新奇的空间:
(显然你根本不用这些)
var num = 7;
const int min = 5;
const int max = 10;
var inRange = Math.Clamp(num, min, max) == num;
Or
var num = 7;
const int min = 5;
const int max = 10;
var inRange = num switch { < min => false, > max => false, _ => true };
Or
var num = 7;
const int min = 5;
const int max = 10;
var inRange = num is >= min and <= max;
好吧,也许你可以用最后一个。
好的,再来一个
var num = 7;
const int min = 5;
const int max = 10;
var inRange = Enumerable.Range(min, max-min).Contains(num);
其他回答
有很多选择:
int x = 30;
if (Enumerable.Range(1,100).Contains(x)) //true
实际上,基本的,如果更优雅的话,可以在第一张支票中用倒序写:
if (1 <= x && x <= 100) //true
此外,查看这篇SO帖子的正则表达式选项。
注:
LINQ solution is strictly for style points - since Contains iterates over all items its complexity is O(range_size) and not O(1) normally expected from a range check. More generic version for other ranges (notice that second argument is count, not end): if (Enumerable.Range(start, end - start + 1).Contains(x) There is temptation to write if solution without && like 1 <= x <= 100 - that look really elegant, but in C# leads to a syntax error "Operator '<=' cannot be applied to operands of type 'bool' and 'int'"
static class ExtensionMethods
{
internal static bool IsBetween(this double number,double bound1, double bound2)
{
return Math.Min(bound1, bound2) <= number && number <= Math.Max(bound2, bound1);
}
internal static bool IsBetween(this int number, double bound1, double bound2)
{
return Math.Min(bound1, bound2) <= number && number <= Math.Max(bound2, bound1);
}
}
使用
double numberToBeChecked = 7;
var result = numberToBeChecked.IsBetween(100,122);
var result = 5.IsBetween(100,120);
var result = 8.0.IsBetween(1.2,9.6);
如果您关心@Daap对已接受答案的注释,并且只能传递一次值,则可以尝试以下方法之一
bool TestRangeDistance (int numberToCheck, int bottom, int distance)
{
return (numberToCheck >= bottom && numberToCheck <= bottom+distance);
}
//var t = TestRangeDistance(10, somelist.Count()-5, 10);
or
bool TestRangeMargin (int numberToCheck, int target, int margin)
{
return (numberToCheck >= target-margin && numberToCheck <= target+margin);
}
//var t = TestRangeMargin(10, somelist.Count(), 5);
这些是一些可以提供帮助的扩展方法
public static bool IsInRange<T>(this T value, T min, T max)
where T : System.IComparable<T>
{
return value.IsGreaterThenOrEqualTo(min) && value.IsLessThenOrEqualTo(max);
}
public static bool IsLessThenOrEqualTo<T>(this T value, T other)
where T : System.IComparable<T>
{
var result = value.CompareTo(other);
return result == -1 || result == 0;
}
public static bool IsGreaterThenOrEqualTo<T>(this T value, T other)
where T : System.IComparable<T>
{
var result = value.CompareTo(other);
return result == 1 || result == 0;
}
好吧,我会配合的。已经有这么多答案了,但也许还有一些其他新奇的空间:
(显然你根本不用这些)
var num = 7;
const int min = 5;
const int max = 10;
var inRange = Math.Clamp(num, min, max) == num;
Or
var num = 7;
const int min = 5;
const int max = 10;
var inRange = num switch { < min => false, > max => false, _ => true };
Or
var num = 7;
const int min = 5;
const int max = 10;
var inRange = num is >= min and <= max;
好吧,也许你可以用最后一个。
好的,再来一个
var num = 7;
const int min = 5;
const int max = 10;
var inRange = Enumerable.Range(min, max-min).Contains(num);