1

有一种方法接收一个 int 参数并通过一组if...else语句检查参数来返回一个字符串:

if(param == 1)
{
    return "1";
}
else if(param ==20)
{
    return "20";
}
else
{
    if(param <= 10)
    {
        return "below 10";
    }
    else if(param <= 30 )
    {
        return "below 30";
    }
    ...
}

我想知道是否可以将这些 ">= , <=" 条件放在字典中

4

5 回答 5

3

您可以使用Dictionary<Func<int, bool>, string>

    private string Do(int input)
    {
        var dic = new Dictionary<Func<int, bool>, string>
            {
                {param => param == 1, "1"},
                {param => param == 20, "20"},
                {param => param <= 10, "below 10"},
                {param => param <= 30, "blow 30"}
            };

        return dic.First(pair => pair.Key(input)).Value;  
    }

编辑:

@Maarten 的评论是正确的,Dictionary不保证项目的顺序,在这种情况ListKeyValuePair应该是最好的:

    private string Do(int input)
    {
        var pairs = new List<KeyValuePair<Func<int, bool>, string>>
            {
                {param => param == 1, "1"},
                {param => param == 20, "20"},
                {param => param <= 10, "below 10"},
                {param => param <= 30, "blow 30"}
            };

        var pair = pairs.FirstOrDefault(pair => pair.Key(input));
        if (pair == null) return string.Empty; // return whatever you want

        return pair.Value;
    }
于 2012-10-02T07:15:52.777 回答
0

可以使用,例如,字典。

只是伪代码的一个例子:

var dic = new Dictionary<int,string>{{1,"1"}, {20, "20"}...}

而不是长if

string value = null; 
dic.TryGetValue(param, out value);
于 2012-10-02T06:49:21.890 回答
0

您的操作不是为字典设计的。考虑以下两种情况

param == 29 and param ==28

两者都会给出“低于 30”的输出。如果参数变量的范围很大,那么您必须将所有可能的值和相应的输出字符串手动放入字典中。这似乎是个好主意???

于 2012-10-02T06:59:07.880 回答
0

不,字典不是为此而设计的。如果你真的有太多的比较条件,你可以把比较对象放在一个数组或列表中,将值按相应的顺序放在另一个数组/列表中,然后你使用二进制搜索找到键的索引,并使用索引来获取值。这是一个例子:

    static string find(int val)
    {
        int[] keys = {30,40,50};
        string[] messages = {"Below 30","Below 40","Below 50"};
        int idx = Array.BinarySearch(keys,val);
        if(idx < 0)idx = ~idx;
        return idx < 3 ? messages[idx] : "Off the chart!";
    }

    public static void Main (string[] args)
    {
        Console.WriteLine (find(28));
        Console.WriteLine (find(50));
        Console.WriteLine (find(100));
    }
于 2012-10-02T07:23:20.420 回答
0

我累了以下。如果您遇到任何问题,请告诉我们:

            int testInput = 15;

            Func<int, bool> delegateForCondition1 = param => param == 1;
            var conditionsSet = new HashSet<KeyValuePair<Func<int, bool>, String>> 
                        { 
                          new KeyValuePair<Func<int, bool>, String>(delegateForCondition1, "It is 1"), 
                          new KeyValuePair<Func<int, bool>, String>(param => param <= 10 , "below 10"),
                          new KeyValuePair<Func<int, bool>, String>(param => param <= 30 , "below 30")
                        };


            foreach (KeyValuePair<Func<int, bool>, String> pair in conditionsSet)
            {
                Func<int, bool> currentKeyAsDelegate = pair.Key;
                bool currentResult = pair.Key(testInput);
                Console.WriteLine(currentKeyAsDelegate + "---" + currentResult);
            }

            //Select the first matching condition
            KeyValuePair<Func<int, bool>, String> selectedPair = conditionsSet.FirstOrDefault(p => p.Key(testInput));
            if (selectedPair.Key != null)
            {
                Console.WriteLine(selectedPair.Value);
            }


            Console.ReadLine();
于 2014-01-22T10:20:26.913 回答