Is there an inter-function in C #?

Google does not understand that the “between” is the name of the function I'm looking for and returns nothing.

Example: I want to check if 5 is between 0 and 10 in only one operation

+54
c #
Feb 16 2018-11-22T00:
source share
17 answers

No, but you can write your own:

public static bool Between(this int num, int lower, int upper, bool inclusive = false) { return inclusive ? lower <= num && num <= upper : lower < num && num < upper; } 
+73
Feb 16 '11 at 23:04
source share

It's not clear what you mean by “one operation”, but no, there is no operator / framework method that I know to determine if an element is within a range.

Of course, you yourself can write an extension method. For example, here, where it is assumed that the interval is closed at both ends.

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

And then use it like:

 bool b = 5.IsBetween(0, 10); // true 
+104
Feb 16 2018-11-11T00:
source share

Here is the full class.

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

plus unit test code

 [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)); } } 
+25
Nov 20 '12 at 9:27
source share

No, you have to test each endpoint separately.

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

Or, if you want it to look more “in between,” reverse the order of the arguments:

 if ((0 < x) && (x < 10)) { // do stuff } 
+20
Feb 16 2018-11-11T00:
source share

Until now, it seems that not one of the answers considered a probable possibility that dynamically you do not know what value is the lower and upper bound. In general, you can create your own IsBetween method, which will probably look something like this:

  public bool IsBetween(double testValue, double bound1, double bound2) { return (testValue >= Math.Min(bound1,bound2) && testValue <= Math.Max(bound1,bound2)); } 
+14
Jun 13 2018-12-12T00:
source share

There is no built-in construct in C # /. NET, but you can easily add your own extension method for this:

 public static class ExtensionsForInt32 { public static bool IsBetween (this int val, int low, int high) { return val > low && val < high; } } 

What can be used as:

 if (5.IsBetween (0, 10)) { /* Do something */ } 
+9
Feb 16 2018-11-11T00:
source share

General function that is checked at compilation!

 public static bool IsBetween<T>(this T item, T start, T end) where T : IComparable { return item.CompareTo(start) >= 0 && item.CompareTo(end) <= 0; } 
+5
Jun 28 '12 at 21:10
source share

It wouldn't be as simple as

 0 < 5 && 5 < 10 

?

So, I suggest that if you want a function to come out of it, you can simply add this to the utility class:

 public static bool Between(int num, int min, int max) { return min < num && num < max; } 
+3
Feb 16 2018-11-11T00:
source share
 int val_to_check = 5 bool in_range = Enumerable.Range(0, 13).Contains(val_to_check); 

The second parameter is the “count”, not the end or high number.

those.

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

Checks if val_to_check is between 0 and 12

+3
Sep 16 '13 at 1:13
source share

With the exception of the answer from @Ed G, all answers require you to know which border is lower and which is the upper.

Here's a (rather unobvious) way to do this when you don't know which border exactly.

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

Note. This is NOT a general method; this is pseudo code. T in the above method should be replaced with the corresponding type "int" or "float" or something else. (There are ways to do this generic, but they are dirty enough that you should not use it for a single-line method, at least in most situations.)

+3
Nov 03 '13 at 6:16
source share

As @Hellfrost noted, it is literally pointless to compare a number with two different numbers in a “single operation”, and I don’t know what the C # operator that encapsulates it is.

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

About the most compact form that I can think of.

You can make a somewhat “smooth” way of observing using the extension (although, although funny, I think it's too much):

 public static bool Between(this int x, int a, int b) { return (a < x) && (x < b); } 

Using:

 int x = 5; bool b = x.Between(0,10); 
+1
Feb 16 2018-11-11T00:
source share

Refer to this link. Single line solution to this issue.

How to elegantly check if a number is within a range?

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

I know the message is quite old, but it may help others.

+1
Oct 09 '15 at 4:40
source share

I do not know this function; In any case, if your value is unsigned, only one operation means (val <11) ... If it is signed, I think there is no atomic way to do this, because 10 is not a power of 2 ...

0
Feb 16 2018-11-11T00:
source share

What about

 somenumber = Math.Max(0,Math.Min(10,somenumber)); 
0
Aug 29 '15 at 10:15
source share

Base @Dan J this version does not care about maximum / min, more like 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) ; } 
0
Dec 29 '15 at 7:41
source share

Or, if you want to associate a value with an interval:

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

It looks like a double-sided Math.Min / Max

0
Mar 28 '16 at 17:40
source share

And for negative values:

  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 
0
Feb 24 '17 at 11:18
source share



All Articles