Common InBetween Function

I'm tired of writing x > min && x < max , so I have to write a simple function, but I'm not sure that I am doing it right ... in fact, I do not exclude that I get an error message:

  bool inBetween<T>(T x, T min, T max) where T:IComparable { return (x > min && x < max); } 

errors:

 Operator '>' cannot be applied to operands of type 'T' and 'T' Operator '<' cannot be applied to operands of type 'T' and 'T' 

Perhaps I have a poor understanding of the where part in the function declaration

Note: for those who are going to tell me that I will write more code than before ... think about readability =) any help would be appreciated

EDIT

deleted because it was allowed =)

OTHER IMAGE

so after some headache I came out with this (ummm) thing after @Jay Idea of โ€‹โ€‹readability:

 public static class test { public static comparision Between<T>(this T a,T b) where T : IComparable { var ttt = new comparision(); ttt.init(a); ttt.result = a.CompareTo(b) > 0; return ttt; } public static bool And<T>(this comparision state, T c) where T : IComparable { return state.a.CompareTo(c) < 0 && state.result; } public class comparision { public IComparable a; public bool result; public void init<T>(T ia) where T : IComparable { a = ia; } } } 

now you can compare something with extreme readability =)

Do you think I'm not a performance guru, so any tricks are welcome

+4
source share
5 answers

IComparable means that the object implements the CompareTo method. Use

 public static bool InBetween<T>(this T x, T min, T max) where T:IComparable<T> { return x.CompareTo(min) > 0 && x.CompareTo(max) < 0; } 
+5
source

You need to use the .CompareTo method of your variable and check for <and> 0. (That's why you limited T to IComparable).

 return (x.CompareTo(min) > 0 && x.CompareTo(max) < 0); 
+3
source
 return x.CompareTo(min) > 0 && x.CompareTo(max) < 0; 

If you want maximum readability, you can use the extension method on IComparable<T> and create syntax, for example:

 return 5.IsBetween(10).and(20); 

or

 return 5.IsBetween(10.and(20)); 

Here is the implementation for the second example:

 public interface IRange<T> { bool ContainsInclusive(T value); bool ContainsExclusive(T value); } public class ComparableRange<T> : IRange<T> where T : IComparable<T> { T min; T max; public ComparableRange(T min, T max) { this.min = min; this.max = max; } public bool ContainsInclusive(T value) { return value.CompareTo(min) >= 0 && value.CompareTo(max) <= 0; } public bool ContainsExclusive(T value) { return value.CompareTo(min) > 0 && value.CompareTo(max) < 0; } } public static class ComparableExtensions { public static IRange<T> and<T>(this T min, T max) where T : IComparable<T> { return new ComparableRange<T>(min, max); } public static bool IsBetween<T>(this T value, IRange<T> range) where T : IComparable<T> { return range.ContainsExclusive(value); } } 
+1
source

Use IComparable.CompareTo () :

a.CompareTo (b)> 0 <=> a> b

a.CompareTo (b) = 0 <=> a = b

a.CompareTo (b) <0 <=> a <b

 public bool InBetween<T>(T x, T min, T max) where T:IComparable { return x.CompareTo(min) * x.CompareTo(max) < 0; } 
0
source

You can try adding a constraint that T is IComparable, and then using CompareTo instead of <and>.

But this probably will not allow you to send all types of values โ€‹โ€‹that you might want. In this case, just create the overloads you need without generics.

0
source

Source: https://habr.com/ru/post/1335585/


All Articles