if / endif在C#中检测到“无法访问的代码”

Sha*_*ann -1 c# unreachable-code

本地一切正常,但出现“无法检测到代码”错误。

这是这段代码:

private string GetRedirectUriForCurrentConfiguration()
{
    #if (DEBUG || DebugDev)
        return "http://localhost:1855/";
    #endif
    return "http://172.16.40.39:1855";
}
Run Code Online (Sandbox Code Playgroud)

我在第四行收到“无法访问”消息 return "http://172.16.40.39:1855";

此语句设置正确吗?

Bri*_*ian 6

只需#else在代码中添加预处理器指令即可:

private string GetRedirectUriForCurrentConfiguration() {

#if (DEBUG || DebugDev)
    return "http://localhost:1855/";
#else
    return "http://172.16.40.39:1855";
#endif  
}
Run Code Online (Sandbox Code Playgroud)

  • 对。在调试模式下,OP的代码等效于2个连续的`return`语句。这样,无论调试模式与否,它都是一个或另一个。因此,没有警告。 (5认同)