C#中有“之间”函数吗?

谷歌不明白,“之间”是我正在寻找的function的名称,并返回没有任何相关的。

例如:我想在一次操作中检查5是否在0和10之间

不,但你可以自己写:

public static bool Between(this int num, int lower, int upper, bool inclusive = false) { return inclusive ? lower <= num && num <= upper : lower < num && num < upper; } 

目前还不清楚“一个操作”是什么意思,但不是,我没有确定一个项目是否在一个范围内的操作员/框架方法。

你当然可以自己写一个扩展方法。 例如,下面是一个假设间隔在两个端点都closures的例子。

 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 

这是一个完整的课程。

 /// <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); } } 

加上一些unit testing代码

 [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)); } } 

不,你必须单独testing每个端点。

 if ((x > 0) && (x < 10)) { // do stuff } 

或者,如果你想让它看起来更“有点”,重新排列参数:

 if ((0 < x) && (x < 10)) { // do stuff } 

到目前为止,似乎没有一个答案已经考虑了dynamic的可能性,你不知道哪个值是下限和上限。 对于一般情况,您可以创build自己的IsBetween方法,可能会像这样:

  public bool IsBetween(double testValue, double bound1, double bound2) { return (testValue >= Math.Min(bound1,bound2) && testValue <= Math.Max(bound1,bound2)); } 

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 */ } 

在编译时validation的通用函数!

 public static bool IsBetween<T>(this T item, T start, T end) where T : IComparable { return item.CompareTo(start) >= 0 && item.CompareTo(end) <= 0; } 
 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之间

除了@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, ie, 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应该用适当的types“int”或“float”来代替。 (有一些通用的方法,但它们非常麻烦,单行方法不值得,至less在大多数情况下是不行的。)

难道不是那么简单吗?

 0 < 5 && 5 < 10 

所以我想如果你想要一个function,你可以简单地把它添加到一个实用程序类:

 public static bool Between(int num, int min, int max) { return min < num && num < max; } 

正如@Hellfrost所指出的那样,在一个操作中将一个数字与两个不同的数字进行比较实际上是无稽之谈,而且我知道没有C#操作符封装了这个。

 between = (0 < 5 && 5 < 10); 

是我能想到的最紧凑的forms。

你可以使用扩展 (虽然有趣,但我认为它是过度的)有点“stream利”的观点方法:

 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); 

参考这个链接。 这个问题的一个解决scheme。

如何优雅地检查一个数字是否在一个范围内?

 int x = 30; if (Enumerable.Range(1,100).Contains(x)) //true if (x >= 1 && x <= 100) //true 

我知道这个post很老,但是可以帮助别人。

我不知道这个function; 无论如何,如果你的价值是无符号的,只有一个操作手段(VAL <11)…如果它被签名,我认为没有primefaces的方式来做到这一点,因为10不是2的幂。

关于什么

 somenumber = Math.Max(0,Math.Min(10,somenumber)); 

Base @Dan J这个版本不关心max / min,更喜欢sql 🙂

 public static bool IsBetween(this decimal me, decimal a, decimal b, bool include = true) { var left = Math.Min(a, b); var righ = Math.Max(a, b); return include ? (me >= left && me <= righ) : (me > left && me < righ) ; } 

或者,如果您想要将值限制在间隔内:

 public static T EnsureRange<T>(this T value, T min, T max) where T : IComparable<T> { if (value.CompareTo(min) < 0) return min; else if (value.CompareTo(max) > 0) return max; else return value; } 

这就像双面Math.Min / Max

对于负值:

  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