Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';我不明白这一点;“检测到无法访问的代码”;错误_C# - Fatal编程技术网

C# Can';我不明白这一点;“检测到无法访问的代码”;错误

C# Can';我不明白这一点;“检测到无法访问的代码”;错误,c#,C#,我有一段给我带来问题的代码 last break语句给出“检测到无法访问的代码”错误 这在我的程序中造成了一个逻辑缺陷 有人能找出这个问题吗 谢谢 case 127232: case1 = splited[0]; changeRow[inServer] = "Audit Privileges and Logs:\n"; changeRow[inServer + " Exception"] = "No Exce

我有一段给我带来问题的代码

last break语句给出“检测到无法访问的代码”错误

这在我的程序中造成了一个逻辑缺陷

有人能找出这个问题吗

谢谢

        case 127232:
            case1 = splited[0];
            changeRow[inServer] = "Audit Privileges and Logs:\n";
            changeRow[inServer + " Exception"] = "No Exception";
            writeToFile(Directory.GetCurrentDirectory() + @"\" + inServer + "127232.txt", case1);


            if (case1.Contains("audit level;failure") || case1.Contains("audit level;all"))
                {
                    Console.WriteLine("success!");
                        changeRow[inServer + "Observations"] = changeRow[inServer + "Observations"] + "1) Audit options found .";
                        changeRow[inServer] = changeRow[inServer] + "string pattern audit level;failure or auditlevel;all found";
                        break;
                }

                else 
                {
                    //Audit Privileges should have been in enabled for sys and system accounts or the security administrator.
                    changeRow[inServer + "Observations"] = changeRow[inServer + "Observations"] + "1) No audit options found.";
                    changeRow[inServer] = changeRow[inServer] + "Correct audit options not configured!";
                    changeRow[inServer + " Exception"] = "Exception";
                    break;
                }
            break;

if
子句的
then
else
部分都包含
break
。因此,显然,
if
后面的代码将永远不会执行。

您的
if
else
子句中都有一个
break
,这意味着底部的
break
将永远无法到达

在这种特殊情况下,这并不重要,但编译器似乎没有足够的“智能”来理解这一点。对于编译器来说,这不一定是一种不好的态度,因为这可能被认为是草率的编码实践

只需去掉
if
else
子句中的
break
语句,或者重构为:

case 127232:
    case1 = splited[0];
    changeRow[inServer] = "Audit Privileges and Logs:\n";
    changeRow[inServer + " Exception"] = "No Exception";
    writeToFile(Directory.GetCurrentDirectory() + @"\" + inServer + "127232.txt", case1);

    if (case1.Contains("audit level;failure") || case1.Contains("audit level;all"))
    {
        Console.WriteLine("success!");
        changeRow[inServer + "Observations"] = changeRow[inServer + "Observations"] + "1) Audit options found .";
        changeRow[inServer] = changeRow[inServer] + "string pattern audit level;failure or auditlevel;all found";
        break;
    }

    //Audit Privileges should have been in enabled for sys and system accounts or the security administrator.

    changeRow[inServer + "Observations"] = changeRow[inServer + "Observations"] + "1) No audit options found.";
    changeRow[inServer] = changeRow[inServer] + "Correct audit options not configured!";
    changeRow[inServer + " Exception"] = "Exception";
    break;
你的原创作品与以下作品没有太大区别:

if (someCondition) {
    doSomething();
    return;
} else {
    doSomethingElse();
}
我一直认为更干净的是:

if (someCondition) {
    doSomething();
    return;
}

doSomethingElse();

除非绝对必要,否则我不喜欢被代码标签死:-)因为你的代码将进入
if
else
中,它们都包含一个
break
,这将把你带到
switch
语句之外,最后一个
break
语句是不必要的

在if条件和Else条件中有中断。在else条件之后,它如何到达break?

将一些答案向上投票并接受一些答案如何?由于if和else都有一个break,因此无法到达最终的break。编译器没有弄明白什么?@CodeInChaos,它没有弄明白无法到达的代码是无关的。它与序列
返回没有区别;返回-尽管第二个
返回值在这种情况下是不可访问的,但它对代码的行为没有影响。不可访问的代码对代码没有影响。我希望
返回;返回
给出完全相同的警告。@code,关于无法访问的代码永远不会产生效果,您当然是对的。也许我应该说“可能的预期效果”。例如,虽然我认为双中断或双返回无害(虽然仍然值得质疑编码器的健全性),序列<代码>返回;x=7;返回我会更加谨慎。很明显,编码人员打算在那里做些什么(第一次返回可能是有条件的),只是不确定是什么:-)更新了答案,补充说这可能是一个问题,即使双中断可能被认为是“无害的”。