开发者

Generic InBetween Function

I am tired of writing x > min && x < max so i wawnt to write a simple function but I am not sure if I am doing it right... actually I am not cuz I get an error:

    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'  

may I have a bad understanding of the where part in the function declaring

note: for those who are going to tell me that I will be writing more code than before... think on readability =) any help will be appreciated

EDIT

deleted cuz it was resolved =)

ANOTHER EDIT

so after some headache I came out with this (ummm) thing following @Jay Idea of extreme 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 anything with extreme readability =)

what do you think.. I am no performance guru so any tweaks are welcome


IComparable means the object implements a 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;
}


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

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


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

If you're going for maximum readability, you could use an extension method on IComparable<T>, 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<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);
    }
}


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


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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜