在以下代码中简化Switch语句

时间:2015-11-17 16:10:47

标签: c# switch-statement code-size

如何让这段代码变得更小,效率更高:) .. 到目前为止,我已经从一个更大的旧代码中做到了,但我认为它仍然是巨大的。

if (affix == Mod.Affix)
{
    Graphics.DrawText(text, textSize, position, Color.White);

    switch (levels)
    {
        case 1:
        {
            Size level = Graphics.DrawText(text, textSize, position, Color.Yellow);
            if (level != new Size()) 
            { 
                position.Y += level.Height; 
            }
        } break;

        case 2:
        {
            Size level = Graphics.DrawText(text, textSize, position, Color.Red);
            if (level != new Size()) 
            {
                position.Y += level.Height; 
            }
        } break;

        case 3:
        {
            Size level = Graphics.DrawText(text, textSize, position, Color.Green);
            if (level != new Size()) 
            { 
                position.Y += level.Height; 
            }
        } break;

        default:
            Size nextLevel = Graphics.DrawText(text, textSize, position, Color.Black);

            if (nextLevel != new Size()) 
            { 
                position.Y += nextLevel.Height;
            } 
        break;
    }
}

提前谢谢!

5 个答案:

答案 0 :(得分:4)

尝试这样的事情:

"id"

答案 1 :(得分:4)

使用字典将关卡映射到颜色:

private static Dictionary<int, Color> levelColors = new Dictionary<int, Color>
{
    { 1, Color.Yellow },
    { 2, Color.Red },
    { 3, Color.Green }
};

然后你可以改变你的方法:

Color color;
if (!levelColors.TryGetValue(levels, out color)) // try and get the color for the level
{
    color = Color.Black; // Default to black if no level color found
}

Size level = Graphics.DrawText(text, textSize, position, color);
if (level != new Size())
{
    position.Y += level.Height;
}

这样,您不需要在添加/更改关卡颜色时修改方法,只需更新字典即可。

答案 2 :(得分:3)

您为每种颜色执行相同的代码,请尝试以下操作:

switch (levels)
{
    case 1:
        AddHeight(Color.Yellow);
        break;
    case 2:
        AddHeight(Color.Red);
        break;
    case 3:
        AddHeight(Color.Green);
        break;
    default:
        AddHeight(Color.Black);
        break;
}

public void AddHeight(Color color){
    Size level = Graphics.DrawText(text, textSize, position, color);
    if (level != new Size()) // ???
    { 
        position.Y += level.Height;
    }
}

答案 3 :(得分:0)

你可以有一个预先映射的&#34; level_to_color&#34; like(例如在静态构造函数中初始化):

Dictionary<int,Color> _levelToColor = new Dictionary<int,Color>();
_levelToColor.Add(1, Color.Yellow);
_levelToColor.Add(2, Color.Red);
_levelToColor.Add(3, Color.Green);

然后您的代码可能如下所示:

Color color = _levelToColor.ContainsKey( level ) ?  _levelToColor[level] : Color.Black;
Size level = Graphics.DrawText(text, textSize, position, color);
position.Y += level.Height;

答案 4 :(得分:-2)

if (affix == Mod.Affix)
{
    Graphics.DrawText(text, textSize, position, Color.White);

    Size level =
         (levels == 1) ? Graphics.DrawText(text, textSize, position, Color.Yellow) 
         :((levels == 2) ? Graphics.DrawText(text, textSize, position, Color.Red) 
         : ((levels == 3) ? Graphics.DrawText(text, textSize, position, Color.Green) 
         : Graphics.DrawText(text, textSize, position, Color.Black)));

    if (level != new Size()) 
    { 
        position.Y += nextLevel.Height;
    }
}