Google不了解“间”是我要查找的函数的名称,并且不返回任何相关内容。
例如:我只想检查一次操作中5是否在0到10之间
Google不了解“间”是我要查找的函数的名称,并且不返回任何相关内容。
例如:我只想检查一次操作中5是否在0到10之间
Answers:
不清楚“一个操作”是什么意思,但是不,我不知道确定某个项目是否在范围内的运算符/框架方法。
您当然可以自己编写扩展方法。例如,这里假设间隔在两个端点上都是闭合的。
public static bool IsBetween<T>(this T item, T start, T end)
{
return Comparer<T>.Default.Compare(item, start) >= 0
&& Comparer<T>.Default.Compare(item, end) <= 0;
}
然后将其用作:
bool b = 5.IsBetween(0, 10); // true
IComparable<T>或的类型,这将如何表现IComparable?
where T : IComparable, IComparable<T>
return Comparer<T>.Default.Compare(item, start) == -Comparer<T>.Default.Compare(item, end);
这是一堂完整的课。
/// <summary>
/// An extension class for the between operation
/// name pattern IsBetweenXX where X = I -> Inclusive, X = E -> Exclusive
/// <a href="https://stackoverflow.com/a/13470099/37055"></a>
/// </summary>
public static class BetweenExtensions
{
/// <summary>
/// Between check <![CDATA[min <= value <= max]]>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">the value to check</param>
/// <param name="min">Inclusive minimum border</param>
/// <param name="max">Inclusive maximum border</param>
/// <returns>return true if the value is between the min & max else false</returns>
public static bool IsBetweenII<T>(this T value, T min, T max) where T:IComparable<T>
{
return (min.CompareTo(value) <= 0) && (value.CompareTo(max) <= 0);
}
/// <summary>
/// Between check <![CDATA[min < value <= max]]>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">the value to check</param>
/// <param name="min">Exclusive minimum border</param>
/// <param name="max">Inclusive maximum border</param>
/// <returns>return true if the value is between the min & max else false</returns>
public static bool IsBetweenEI<T>(this T value, T min, T max) where T:IComparable<T>
{
return (min.CompareTo(value) < 0) && (value.CompareTo(max) <= 0);
}
/// <summary>
/// between check <![CDATA[min <= value < max]]>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">the value to check</param>
/// <param name="min">Inclusive minimum border</param>
/// <param name="max">Exclusive maximum border</param>
/// <returns>return true if the value is between the min & max else false</returns>
public static bool IsBetweenIE<T>(this T value, T min, T max) where T:IComparable<T>
{
return (min.CompareTo(value) <= 0) && (value.CompareTo(max) < 0);
}
/// <summary>
/// between check <![CDATA[min < value < max]]>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">the value to check</param>
/// <param name="min">Exclusive minimum border</param>
/// <param name="max">Exclusive maximum border</param>
/// <returns>return true if the value is between the min & max else false</returns>
public static bool IsBetweenEE<T>(this T value, T min, T max) where T:IComparable<T>
{
return (min.CompareTo(value) < 0) && (value.CompareTo(max) < 0);
}
}
加上一些单元测试代码
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethodIsBeetween()
{
Assert.IsTrue(5.0.IsBetweenII(5.0, 5.0));
Assert.IsFalse(5.0.IsBetweenEI(5.0, 5.0));
Assert.IsFalse(5.0.IsBetweenIE(5.0, 5.0));
Assert.IsFalse(5.0.IsBetweenEE(5.0, 5.0));
Assert.IsTrue(5.0.IsBetweenII(4.9, 5.0));
Assert.IsTrue(5.0.IsBetweenEI(4.9, 5.0));
Assert.IsFalse(5.0.IsBetweenIE(4.9, 5.0));
Assert.IsFalse(5.0.IsBetweenEE(4.9, 5.0));
Assert.IsTrue(5.0.IsBetweenII(5.0, 5.1));
Assert.IsFalse(5.0.IsBetweenEI(5.0, 5.1));
Assert.IsTrue(5.0.IsBetweenIE(5.0, 5.1));
Assert.IsFalse(5.0.IsBetweenEE(5.0, 5.1));
Assert.IsTrue(5.0.IsBetweenII(4.9, 5.1));
Assert.IsTrue(5.0.IsBetweenEI(4.9, 5.1));
Assert.IsTrue(5.0.IsBetweenIE(4.9, 5.1));
Assert.IsTrue(5.0.IsBetweenEE(4.9, 5.1));
Assert.IsFalse(5.0.IsBetweenII(5.1, 4.9));
Assert.IsFalse(5.0.IsBetweenEI(5.1, 4.9));
Assert.IsFalse(5.0.IsBetweenIE(5.1, 4.9));
Assert.IsFalse(5.0.IsBetweenEE(5.1, 4.9));
}
}
C#/。NET中没有内置的构造,但是您可以为此轻松添加自己的扩展方法:
public static class ExtensionsForInt32
{
public static bool IsBetween (this int val, int low, int high)
{
return val > low && val < high;
}
}
可以这样使用:
if (5.IsBetween (0, 10)) { /* Do something */ }
除了@Ed G的答案外,所有答案都需要知道哪个界限是下限,哪个是上限。
当您不知道哪个边界是哪个时,这是一种(相当不明显)的方法。
/// <summary>
/// Method to test if a value is "between" two other values, when the relative magnitude of
/// the two other values is not known, i.e., number1 may be larger or smaller than number2.
/// The range is considered to be inclusive of the lower value and exclusive of the upper
/// value, irrespective of which parameter (number1 or number2) is the lower or upper value.
/// This implies that if number1 equals number2 then the result is always false.
///
/// This was extracted from a larger function that tests if a point is in a polygon:
/// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
/// </summary>
/// <param name="testValue">value to be tested for being "between" the other two numbers</param>
/// <param name="number1">one end of the range</param>
/// <param name="number2">the other end of the range</param>
/// <returns>true if testValue >= lower of the two numbers and less than upper of the two numbers,
/// false otherwise, incl. if number1 == number2</returns>
private static bool IsInRange(T testValue, T number1, T number2)
{
return (testValue <= number1) != (testValue <= number2);
}
注意:这不是通用方法;它是伪代码。上述方法中的T应该用适当的类型“ int”或“ float”或其他任何类型替换。(有很多方法可以使这种通用,但是它们很混乱,以至于单行方法是不值得的,至少在大多数情况下不是这样。)
会不会像
0 < 5 && 5 < 10
?
因此,我想如果您想要一个函数,可以简单地将其添加到实用程序类中:
public static bool Between(int num, int min, int max) {
return min < num && num < max;
}
bool不是void。
int val_to_check = 5
bool in_range = Enumerable.Range(0, 13).Contains(val_to_check);
第二个参数是“计数”,而不是结束数或高位数。
IE浏览器
int low_num = 0
int high_num = 12
int val_to_check = 5
bool in_range = Enumerable.Range(low_num , high_num - low_num + 1).Contains(val_to_check);
检查val_to_check是否在0到12之间
请参考此链接。这个问题的一线解决方案。
int x = 30;
if (Enumerable.Range(1,100).Contains(x))
//true
if (x >= 1 && x <= 100)
//true
我知道该职位已经很老了,但可能会对其他人有所帮助。
正如@Hellfrost指出的那样,在“一个操作”中将一个数字与两个不同的数字进行比较在字面上是胡说八道,而且我知道没有C#运算符对此进行封装。
between = (0 < 5 && 5 < 10);
是我能想到的最紧凑的形式。
您可以使用扩展程序制作一种看上去“流利”的方法(尽管虽然有趣,但我认为这太过分了):
public static bool Between(this int x, int a, int b)
{
return (a < x) && (x < b);
}
采用:
int x = 5;
bool b = x.Between(0,10);
对于负值:
Public Function IsBetween(Of T)(item As T, pStart As T, pEnd As T) As Boolean ' https://msdn.microsoft.com/fr-fr/library/bb384936.aspx
Dim Deb As T = pStart
Dim Fin As T = pEnd
If (Comparer(Of T).Default.Compare(pStart, pEnd) > 0) Then Deb = pEnd : Fin = pStart
Return Comparer(Of T).Default.Compare(item, Deb) >= 0 AndAlso Comparer(Of T).Default.Compare(item, Fin) <= 0
End Function