切换大小写而不是 if else

Switch case instead of if else

如何将此代码转换为 switch 案例?

protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_HOTKEY && (int)m.WParam == 1)
        Console.WriteLine("w");
    else if (m.Msg == WM_HOTKEY && (int)m.WParam == 2)
        Console.WriteLine("a");
    else if (m.Msg == WM_HOTKEY && (int)m.WParam == 3)
        Console.WriteLine("s");
    else if (m.Msg == WM_HOTKEY && (int)m.WParam == 4)
        Console.WriteLine("d");

    base.WndProc(ref m);
}

这是我最后一次尝试,第一个案例中的行再次标记为红色

switch (m.Msg)
{
    case m.Msg == WM_HOTKEY && (int)m.WParam == 1:
       Console.WriteLine("w");
       break;
    case m.Msg == WM_HOTKEY && (int)m.WParam == 2:
       Console.WriteLine("a");
       break;
    case m.Msg == WM_HOTKEY && (int)m.WParam == 3:
       Console.WriteLine("s");
       break;
    case m.Msg == WM_HOTKEY && (int)m.WParam == 4:
       Console.WriteLine("d");
       break;
}

这可能吗?我认为在 switch 语句中阅读会更好看。

不,您不能在 switch case 中使用逻辑运算符。它仅对单个值进行操作

由于 Msg 的值始终相同(在您的条件下),您可以尝试这样做:

if(m.Msg == WM_HOTKEY)
{
    switch ((int)m.WParam)
    {
        case 1:
            Console.WriteLine("w");
            break;
        case 2:
            Console.WriteLine("a");
            break;
        case 3:
            Console.WriteLine("s");
            break;
        case 4:
            Console.WriteLine("d");
            break;
    }
}

关于 switch 语句,了解以下内容(摘自 MSDN)很重要:

Each case label specifies a constant value. The switch statement transfers control to the switch section whose case label matches the value of the switch expression (caseSwitch in the example). If no case label contains a matching value, control is transferred to the default section, if there is one. If there is no default section, no action is taken and control is transferred outside the switch statement.

您无法开启条件。您只能打开常量值。

if (m.Msg == WM_HOTKEY)
{
    switch ((int)m.WParam)
    {
        case 1:
            Console.WriteLine("w");
            break;
        case 2:
            Console.WriteLine("a");
            break;
        case 3:
            Console.WriteLine("s");
            break;
        case 4:
            Console.WriteLine("d");
            break;
    }
}

您在这里唯一能做的就是:

if(m.Msg == WM_HOTKEY)
{
    var param = (int)m.WParam;
    switch(param)
    {
        case 1:
            Console.WriteLine("w");
            break;
            ....
    }
}
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_HOTKEY)
    {
        var param = (int)m.WParam;
        switch (param)
        {
            case 1:
                Console.WriteLine("w");
                break;
            case 2:
                Console.WriteLine("a");
                break;
            case 3:
                Console.WriteLine("s");
                break;
            case 4:
                Console.WriteLine("d");
                break;
            default:
                Console.WriteLine("Unrecognised key stroke.");
        }
        base.WndProc(ref m);
    }
    // todo:  What if m.Msg is no WM_HOTKEY?
}