我的代码中有以下方法.我的老板问"你为什么要使用字典",你可以使用数组,效率更高:
public static Dictionary<string, string> GetListOfMonths()
{
Dictionary<string, string> months = new Dictionary<string, string>();
months.Add("1", "Jan");
months.Add("2", "Feb");
months.Add("3", "Mar");
months.Add("4", "Apr");
months.Add("5", "May");
months.Add("6", "Jun");
months.Add("7", "Jul");
months.Add("8", "Aug");
months.Add("9", "Sep");
months.Add("10", "Oct");
months.Add("11", "Nov");
months.Add("12", "Dec");
return months;
}
Run Code Online (Sandbox Code Playgroud)
我使用它来代码重用.所以我可以绑定到应用程序各个部分的一些月下拉菜单.
Jon*_*eet 19
对于基于整数索引的合理密集集合,数组肯定更有效.
另一方面,数组不会让你使用string
密钥,就像你的字典一样.
回到第一手,为什么你想要字典的字符串键?
我不会在性能方面过于沉溺 - 编写最易读的代码.如果你确定在这里使用字符串键更合适,那么继续使用字典.替代方法(如果您已经提供了一个字符串)是将整数和索引解析为数组.
我们说这将是最好的解决方案并不是真的可行.
您应该对您的老板说:"我使用字典是因为它会导致更易读和可维护的代码.如果在测试期间我们检测到应用程序中的性能问题,并且分析表明它是字典的错误,那么我将尝试使用数组."
归档时间: |
|
查看次数: |
2103 次 |
最近记录: |