C#switch语句返回适合更换突破 [英] c# switch statement is return suitable to replace break

查看:148
本文介绍了C#switch语句返回适合更换突破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个合适的方法来处理C#switch语句或要求仍然是一个明确的突破?

参考

 公共静态字符串ToRegistryString(AliceKey.AliceKeyPaths aliceKeyPath)
    {
     开关(aliceKeyPath)
        {
            案例AliceKey.AliceKeyPaths.NET_CLR_DATA:
                返回@\\ NET CLR数据\\。
            案例AliceKey.AliceKeyPaths.NET_CLR_NETWORKING:
                返回@\\ NET CLR网络\\。
            案例AliceKey.AliceKeyPaths.NET_DATA_PROVIDER_MSSQL:
                返回@\\ NET数据提供的SqlServer \\。
            案例AliceKey.AliceKeyPaths.NET_DATA_PROVIDER_ORACLE:
                返回@\\ NET数据提供程序的Oracle \\。
         }
       返回新的字符串(新的char [0]);
     }


解决方案

这很好。问题的关键是,一个案子块的结尾应该是不可达 - 这正是在这里,因为你已经返回

为什么要退新的字符串(新的char [0]),而不是仅仅或的String.Empty 有关系吗?如果你想以确保它是一个不同的字符串的时候,你会真正碰到一个非常奇怪的角落情况下 - 尽管调用新的字符串(...)的code将始终实际返回相同的参考...

最后:我真的建议改变此开关/ case块逼到了词典< AliceKey.AliceKeyPaths,串>

 私有静态只读字典< AliceKey.AliceKeyPaths,串> RegistryMap =
    新字典< AliceKey.AliceKeyPaths,串>
{
    {AliceKey.AliceKeyPaths.NET_CLR_DATA,@\\ NET CLR数据\\},
    {AliceKey.AliceKeyPaths.NET_CLR_NETWORKING,@\\ NET CLR网络\\},
    //等
};公共静态字符串ToRegistryString(AliceKey.AliceKeyPaths aliceKeyPath)
{
    字符串值;
    返回RegistryMap.TryGetValue(aliceKeyPath,出来的值)?值:;
}

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]);
     }

解决方案

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 : "";
}

这篇关于C#switch语句返回适合更换突破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆