Generic InBetween Function?

IComparable means the object implements a CompareTo method. Use public static bool InBetween(this T x, T min, T max) where T:IComparable { return x. CompareTo(min) > 0 && x.

CompareTo(max).

– Yuriy Faktorovich Jan 14 at 19:11 When you use the non-generic IComparable, it has a .CompareTo() method which expects an object. So in when min or max are passed to it, they need to be objects.(They may be structs for instance, which are not objects, they're valuetypes). If they are structs, they are boxed, which basically creates an object to reference the struct, but this can cost over 10% more in some situations, resulting in slower code (particularly noticeable when used on collections).

– Mark H Jan 14 at 19:46 1 In the case of the generic IComparable, the CompareTo() method expects a T, which is what min and max already are constrained to. No boxing is needed in that case. – Mark H Jan 14 at 19:47 @Sparkie didn't realize I was Constraining to the wrong interface.

Thought they were the same for some reason. Thank you. – Yuriy Faktorovich Jan 14 at 19:53.

You need to use the . CompareTo method of your variable and check for 0. (This is why you've constrained T to IComparable).

Return (x. CompareTo(min) > 0 && x. CompareTo(max).

Return x. CompareTo(min) > 0 && x. CompareTo(max) , and create a syntax like: return 5.

IsBetween(10). And(20); or return 5. IsBetween(10.

And(20)); Here is an implementation for the second example: public interface IRange { bool ContainsInclusive(T value); bool ContainsExclusive(T value); } public class ComparableRange : IRange where T : IComparable { 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 && value. CompareTo(max) ContainsExclusive(value); } }.

Could you show how you'd implement those extensions. – Yuriy Faktorovich Jan 14 at 18:11 still waiting for that golden implementation – Luiscencio Jan 14 at 18:30 @Yuriy @Luiscencio I added an update with an example implementation. This is an exercise; not a recommendation.

You could do the same thing with a tuple, instead of a range, but you'd be putting the actual comparison logic in the extension method. – Jay Jan 14 at 19:00! You actually didi it that is what I call commitment =) – Luiscencio Jan 14 at 19:05 check my version and tell me what you think (edited OP) – Luiscencio Jan 14 at 19:58.

Use IComparable.CompareTo(): a. CompareTo(b) > 0 a>b a. CompareTo(b) = 0 a=b a.

CompareTo(b).

1 Clever choice of multiplication, even if it's not the most transparent solution. – Adam Robinson Jan 14 at 17:57 I'd also be curious as to the performance hit – Yuriy Faktorovich Jan 14 at 18:02 1 Multiplication is not a clever choice. Consider someone else use something like this: bool result = InBetween(5,10,4);, which returns true.So that you need to add a check to make sure max is greater than min, if not then return false, which makes the method more complicated.

– Danny Chen Jan 14 at 18:15.

You can try adding a constraint that T is an IComparable, and then using CompareTo instead of . But this probably won't let you send all the value types you may want to. In that case, just create the overloads you need, without generics.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions