C# switch语句中的return适合代替break

发布于 2024-09-08 05:14:41 字数 782 浏览 3 评论 0原文

这是处理 C# switch 语句的适当方法还是仍然需要显式中断? 参考

  public static string ToRegistryString(AliceKey.AliceKeyPaths aliceKeyPath)
    {
     switch (aliceKeyPath)
        {
            case AliceKey.AliceKeyPaths.NET_CLR_DATA:
                return @"\.NET CLR Data\";
            case AliceKey.AliceKeyPaths.NET_CLR_NETWORKING:
                return @"\.NET CLR Networking\";
            case AliceKey.AliceKeyPaths.NET_DATA_PROVIDER_MSSQL:
                return @"\.NET Data Provider for SqlServer\";
            case AliceKey.AliceKeyPaths.NET_DATA_PROVIDER_ORACLE:
                return @"\.NET Data Provider for Oracle\";
         }
       return new string(new char[0]);
     }

Is this an appropriate way to handle c# switch statements or is an explicit break required still? reference

  public static string ToRegistryString(AliceKey.AliceKeyPaths aliceKeyPath)
    {
     switch (aliceKeyPath)
        {
            case AliceKey.AliceKeyPaths.NET_CLR_DATA:
                return @"\.NET CLR Data\";
            case AliceKey.AliceKeyPaths.NET_CLR_NETWORKING:
                return @"\.NET CLR Networking\";
            case AliceKey.AliceKeyPaths.NET_DATA_PROVIDER_MSSQL:
                return @"\.NET Data Provider for SqlServer\";
            case AliceKey.AliceKeyPaths.NET_DATA_PROVIDER_ORACLE:
                return @"\.NET Data Provider for Oracle\";
         }
       return new string(new char[0]);
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零時差 2024-09-15 05:14:41

没关系。关键是 case 块的末尾应该是无法到达的 - 它就在这里,因为你已经返回了。

为什么你返回 new string(new char[0]) 而不仅仅是 "" 或 string.Empty ?如果你试图确保每次都是不同的字符串,你实际上会遇到一个非常奇怪的极端情况 - 尽管调用 new string(...) 该代码实际上总是返回相同的参考...

最后:我实际上建议将此 switch/case 块更改为 Dictionary

private static readonly Dictionary<AliceKey.AliceKeyPaths, string> RegistryMap =
    new Dictionary<AliceKey.AliceKeyPaths, string>
{
    { AliceKey.AliceKeyPaths.NET_CLR_DATA, @"\.NET CLR Data\" },
    { AliceKey.AliceKeyPaths.NET_CLR_NETWORKING, @"\.NET CLR Networking\" },
    // etc
};

public static string ToRegistryString(AliceKey.AliceKeyPaths aliceKeyPath)
{
    string value;
    return RegistryMap.TryGetValue(aliceKeyPath, out value) ? value : "";
}

That's fine. The point is that the end of a case block should be unreachable - which it is here, because you've returned.

Why are you returning new string(new char[0]) rather than just "" or string.Empty though? If you're trying to make sure it's a different string each time, you'll actually run into a very weird corner case - despite calling new string(...) that code will always actually return the same reference...

Finally: I would actually suggest changing this switch/case block into just a Dictionary<AliceKey.AliceKeyPaths, string>:

private static readonly Dictionary<AliceKey.AliceKeyPaths, string> RegistryMap =
    new Dictionary<AliceKey.AliceKeyPaths, string>
{
    { AliceKey.AliceKeyPaths.NET_CLR_DATA, @"\.NET CLR Data\" },
    { AliceKey.AliceKeyPaths.NET_CLR_NETWORKING, @"\.NET CLR Networking\" },
    // etc
};

public static string ToRegistryString(AliceKey.AliceKeyPaths aliceKeyPath)
{
    string value;
    return RegistryMap.TryGetValue(aliceKeyPath, out value) ? value : "";
}
简单爱 2024-09-15 05:14:41

您不需要专门使用 break 语句,只需使用一个改变控制流的语句,因此 gotoreturn 应该可以工作。

有关详细信息,请参阅 MSDN:http://msdn.microsoft。 com/en-us/library/06tc147t(VS.71).aspx

You do not need to specifically use a break statement just one that changes the flow of control, so a goto or a return should work.

See MSDN for more info: http://msdn.microsoft.com/en-us/library/06tc147t(VS.71).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文