C# 如何重构一组<;=>;=if…else语句输入字典或类似的东西

C# 如何重构一组<;=>;=if…else语句输入字典或类似的东西,c#,asp.net,if-statement,dictionary,refactoring,C#,Asp.net,If Statement,Dictionary,Refactoring,有一种方法接收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"; }

有一种方法接收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";
    }
    ...
}
if(参数==1)
{
返回“1”;
}
否则如果(参数==20)
{
返回“20”;
}
其他的
{

例如,if(param可以使用字典

只是伪代码的一个示例:


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

param == 29 and param ==28

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

您可以使用
字典


不,字典不是为此而设计的。如果确实有太多比较条件,可以将比较对象放入数组或列表中,将值按相应顺序放入另一个数组/列表中,然后使用binarysearch查找键的索引,并使用索引获取值。以下是一个示例:

    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));
    }
静态字符串查找(int-val)
{
int[]键={30,40,50};
字符串[]消息={“低于30”、“低于40”、“低于50”};
int idx=Array.BinarySearch(key,val);
如果(idx<0)idx=~idx;
返回idx<3?消息[idx]:“图表外!”;
}
公共静态void Main(字符串[]args)
{
Console.WriteLine(find(28));
Console.WriteLine(find(50));
Console.WriteLine(find(100));
}

我厌倦了以下内容。如果您在这方面遇到任何问题,请告诉我们:

            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();
int testInput=15;
Func delegateForCondition1=param=>param==1;
var conditionsSet=新哈希集
{ 
新的KeyValuePair(条件1中的delegateForCondition1,“它是1”),
新的KeyValuePair(param=>param param p.Key(testInput));
if(selectedPair.Key!=null)
{
Console.WriteLine(selectedPair.Value);
}
Console.ReadLine();

是的,但如中所述,此方法存在性能问题,是吗?@mohsen.d此方法将执行每个条件,直到第一次匹配。您通常需要字典,因为您希望直接跳转到正确的项,而不经过任何其他项。不过,这是一个聪明的技巧。如果您有一个l,则开销不会太大项目数量有限。此外,字典中项目的顺序也无法保证。因此,您无法预测检查参数是否为抱歉,是的,您正在迭代键,而不是值。您在任何时候都没有使用O(1)不过,查找字典——使用元组列表也可以,这样的列表可以保证排序。(如您刚才所示:))挑剔:如果对值99运行方法,则会出现异常。
    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;  
    }
    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;
    }
    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));
    }
            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();