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
source share