优化长的switch语句
2024-08-31 12:20:22
突然想到之前碰到的一个优化的面试题,现在想想switch用的太傻
public enum FormatType
{
GetKey,
GetValue
}
public class Format
{
private static string Get(Dictionary<string, string> dict, FormatType formatType, string code)
{
if (formatType == FormatType.GetKey)
return dict.FirstOrDefault(d => d.Value == code).Key;
else
return dict.FirstOrDefault(d => d.Key == code).Value;
}
public static string GetNation(FormatType formatType, string code)
{
Dictionary<string, string> dict = new Dictionary<string, string>{
{"","汉族"},
{"","藏族"},
{"","壮族"},
{"","朝鲜族"}
//这里省略其它......
};
return Get(dict, formatType, code);
}
}
使用:
获取"汉族"的编码
string key = Format.GetNation(FormatType.GetKey, "汉族");
获取"01"代表的民族
string value = Format.GetNation(FormatType.GetValue, "01");
--------------------------------------------------------------------------------------------
最新文章
- go并发
- protoc 和 protoc-gen-go 产生的 proto 文件代码对比
- JavaScript 为什么要通过原型 prototype 调用函数, 而不是直接调用?
- noip2006T1 能量项链
- 多线程程序设计学习(4)guarded suspension模式
- Unity 5.4大赞:HTC Vive经典The lab渲染器开源
- C 中随机数
- [iOS] Create TableView &; customize UITableViewCell
- C++程序设计实践指导1.10二维数组元素换位改写要求实现
- Can&#39;t load IA 32-bit .dll on a AMD 64-bit platform
- java/php/c#版rsa签名以及验签实现
- 2014在百度之星资格赛的第二个问题Disk Schedule
- swift 之函数式编程(一)
- 给COCO数据集的json标签换行
- springboot性能优化
- LAB8 android
- Mysql Group by 使用解析
- mysql 安装部署
- MySQL数据查询之多表查询
- Informatic学习总结_day01
热门文章
- ZBrush中Tool工具的保存
- 安装Oracle 12c及解决遇到的问题
- HDU 2078 选课时间( 水题 )
- BZOJ 2141 排队 (三维偏序CDQ+树状数组)
- [读书笔记] Python数据分析 (二) 引言
- C++操作符重载总结operator(小结 更新ing)
- java EE使用response返回中文时,出现乱码问题
- .get(),eq()的区别
- tp volist需要便利两个数组时的处理办法
- 微信支付报ip错,怀疑是因为不能正确获取$_Server[addr])ip导致的