0

I am searching of an elegant way to assign values in function of a number belonging to a specific range.

For example, having the number X, the elegant way would return:

  • 'a' - if X is between 0 and 1000
  • 'b' - if X is between 1000 and 1500
  • and so on (but a fixed number of defined intervals)

By elegant I mean something more appealing than

if ((x => interval_1) && (x < interval_2))
    class_of_x = 'a';
else if ((x => interval_2) && (x < interval_3))
    class_of_x = 'b';
...

or

if(Enumerable.Range(interval_1, interval_2).Contains(x))
    class_of_x = 'a';
else if(Enumerable.Range(interval_2 + 1, interval_3).Contains(x))
    class_of_x = 'b';
...

I hate seeing so many IFs. Also, the interval values can be stored in a collection (maybe this would help me eliminate the ISs?), not necessary as interval_1, interval_2 and so on.

Somewhat inspired by this question How to elegantly check if a number is within a range? which came out while looking for a solution for the problem described above.

4

5 回答 5

1

您可以创建扩展方法:

public static class IntExtensions
{
    // min inclusive, max exclusive
    public static bool IsBetween(this int source, int min, int max)
    {
        return source >= min && source < max
    }
}

接着

// Item1 = min, Item2 = max, Item3 = character class
IList<Tuple<int, int, char>> ranges = new List<Tuple<int, int, char>>();
// init your ranges here
int num = 1;
// assuming that there certainly is a range which fits num,
// otherwise use "OrDefault"
// it may be good to create wrapper for Tuple, 
// or create separate class for your data
char characterClass = ranges.
                        First(i => num.IsBetween(i.Item1, i.Item2)).Item3;
于 2013-08-14T15:17:12.900 回答
1

创建一个 Interval 类并使用 LINQ:

public class Interval
{
    public string TheValue { get; set; }
    public int Start { get; set; }
    public int End { get; set; }

    public bool InRange(int x)
    {
        return x >= this.Start && x <= this.End;
    }
}

public void MyMethod()
{
    var intervals = new List<Interval>();

    // Add them here...

    var x = 3213;
    var correctOne = intervals.FirstOrDefault(i => i.InRange(x));

    Console.WriteLine(correctOne.TheValue);
}
于 2013-08-14T15:25:37.843 回答
1

如果我的评论是正确的,那么你的第一个 if 语句有很多不必要的检查,如果它不小于间隔 2,那么它必须大于或等于,因此:

if((x => i1) && (x < i2))
else if(x < i3)
else if(x < i4)...

当找到“真”参数时,if 语句的其余部分无关紧要,只要您的条件符合要求,这应该适合您的需要

于 2013-08-14T15:15:00.733 回答
1

首先,定义一个小类来保存包含的最大值,以及用于该波段的相应值:

sealed class Band
{
    public int  InclusiveMax;
    public char Value;
}

然后声明一个数组,Band其中指定用于每个波段的值并循环查找任何输入的相应波段值:

public char GetSetting(int input)
{
    var bands = new[]
    {
        new Band {InclusiveMax = 1000, Value = 'a'},
        new Band {InclusiveMax = 1500, Value = 'b'},
        new Band {InclusiveMax = 3000, Value = 'c'}
    };

    char maxSetting = 'd';

    foreach (var band in bands)
        if (input <= band.InclusiveMax)
            return band.Value;

    return maxSetting;
}

注意:在实际代码中,您会将所有这些包装到一个类中,该类只初始化bands一次数组,而不是每次调用它(就像在上面的代码中一样)。

于 2013-08-14T15:21:15.337 回答
0

在这里,您还可以使用实现的静态 System.Linq.Enumerable 的 Range() 方法

IEnumerable<T>

使用 Contains() 方法(同样来自 System.Linq.Enumerable),执行以下操作:

var num = 254;
if(Enumerable.Range(100,300).Contains(num)) { ...your logic here; }

至少在我看来,这看起来更优雅。

于 2013-11-08T09:07:48.717 回答