编写这段代码的有效方法

时间:2015-06-10 10:34:15

标签: c# performance

有经验的程序员如何写这个......

SummonerPlatform = (SummonerRegion == Region.br) ? SummonerPlatform = Platform.BR1 : (SummonerRegion == Region.eune) ? SummonerPlatform = Platform.EUN1 :
                                (SummonerRegion == Region.euw) ? SummonerPlatform = Platform.EUW1 : (SummonerRegion == Region.kr) ? SummonerPlatform = Platform.KR :
                                (SummonerRegion == Region.lan) ? SummonerPlatform = Platform.LA1 : (SummonerRegion == Region.las) ? SummonerPlatform = Platform.LA2 :
                                (SummonerRegion == Region.na) ? SummonerPlatform = Platform.NA1 : (SummonerRegion == Region.oce) ? SummonerPlatform = Platform.OC1 :
                                (SummonerRegion == Region.ru) ? SummonerPlatform = Platform.RU : (SummonerRegion == Region.tr) ? SummonerPlatform = Platform.TR1 : SummonerPlatform = Platform.EUW1;

1 个答案:

答案 0 :(得分:7)

使用这样的switch()语句:

 switch (SummonerRegion)
            {
                case Region.br:
                    SummonerPlatform = Platform.BR1;
                    break;
                case Region.eune:
                    SummonerPlatform = Platform.EUN1;
                    break;
                case ...
            }

或创建这样的映射:

static Dictionary<Region, Platform> Mapping = new Dictionary<Region, Platform> { 
  {Region.br, Platform.BR1}, 
  {Region.eune, Platform.EUN1}, 
  ...
};

然后在你的代码中使用它:

SummonerPlatform=Mapping[SummonerRegion];

只有Region是枚举时,switch()方法才有效。如果没有,请使用第二种方法