c# – 使用List,Lookup或Dictionary来获取大量数据

前端之家收集整理的这篇文章主要介绍了c# – 使用List,Lookup或Dictionary来获取大量数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的类库中有一个名为Lookup的静态类,我使用这个类来查找不同的值(在本例中为Locations).

这些值可以达到数百个.由于95%的客户在没有Internet访问权限的计算机上安装我的应用程序,因此我必须假设我的应用程序无法访问Internet或访问数据库.

所以我想知道这是否是一种有效的处理方法,如果我在方法完成后正确处理对象:

代码

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace FunctionLibrary
  5. {
  6. public static class Lookups
  7. {
  8. private static List<Vers> Versions;
  9.  
  10. public static string GetVersion(string s)
  11. {
  12. string retValue = string.Empty;
  13. Versions = new List<Vers>();
  14.  
  15. try
  16. {
  17. if (s.Trim().Length > 0)
  18. {
  19.  
  20. GetVersions();
  21. retValue = Versions.Find(ver => ver.VersionNumber == s).VersionLiteral;
  22.  
  23. if (string.IsNullOrEmpty(retValue))
  24. {
  25. retValue = string.Format("{0} is an Unknown Version Number",s);
  26. }
  27. }
  28. else
  29. {
  30. retValue = "No version number supplied";
  31. }
  32. }
  33. catch
  34. {
  35. retValue = string.Format("{0} is an Unknown Version Number",s);
  36. }
  37. finally
  38. {
  39. Versions.Clear();
  40. Versions = null;
  41. }
  42. return retValue;
  43. }
  44.  
  45. private static void GetVersions()
  46. {
  47. Versions.Add(new Vers() { VersionNumber = "0000",VersionLiteral = "Location 1" });
  48. Versions.Add(new Vers() { VersionNumber = "0001",VersionLiteral = "Location 2" });
  49. Versions.Add(new Vers() { VersionNumber = "0002",VersionLiteral = "Location 3"});
  50. Versions.Add(new Vers() { VersionNumber = "0003",VersionLiteral = "Location 4"});
  51. Versions.Add(new Vers() { VersionNumber = "0004",VersionLiteral = "Location 5"});
  52. Versions.Add(new Vers() { VersionNumber = "0005",VersionLiteral = "Location 6"});
  53. Versions.Add(new Vers() { VersionNumber = "0006",VersionLiteral = "Location 7"});
  54. Versions.Add(new Vers() { VersionNumber = "0007",VersionLiteral = "Location 8"});
  55. }
  56. }
  57.  
  58. public class Vers
  59. {
  60.  
  61. public string VersionLiteral { get; set; }
  62. public string VersionNumber { get; set; }
  63. }
  64. }

我也想知道我是否应该使用Dictionary或Lookup而不是列表.我只是不希望多次调用方法导致内存问题.

解决方法

要进行更全面的评估,您可能需要考虑 codereview.SE.

关于List<T> vs Dictionary<TKey,TValue> vs Lookup<TKey,TElement>的一些一般性说明

正如其他答案所示,在您的场景中使用List非常糟糕,主要是因为查找元素会有不良表现.

在Dictionary和Lookup之间选择并不难(从MSDN开始,强调我的):

A Lookup<TKey,TElement> resembles a Dictionary<TKey,TValue>. The difference is
that a Dictionary<TKey,TValue> maps keys to single values,whereas a
Lookup<TKey,TElement> maps keys to collections of values.

You can create an instance of a Lookup<TKey,TElement> by calling ToLookup
on an object that implements IEnumerable<T>.

由于您只需要将键映射到单个值,因此Dictionary是正确的选择.

previously accepted answer是朝着正确方向迈出的一步,但仍然会出现几个关键问题(编辑:这些问题已经解决).

字符串是不可变的:s.Trim()不会改变s – 它将返回一个新字符串,这意味着你需要s = s.Trim()如果你之后使用s,那就是你.

静态类不能有实例构造函数:public Lookups()应该是静态的Lookups()(当然不允许静态构造函数具有访问修饰符).

不要将空字符串/错误消息作为字符串返回!

这将成为一个奇妙的调试头痛.您应该使用异常而不是传递错误字符串 – 并且您应该提供VersionExists方法来检查您的字典是否包含特定版本!

修改,更安全的例子

如果参数为空,null或空格,则抛出FormatException.如果版本不存在,Dictionary会抛出一个KeyNotFoundException – 比string.Empty对调试更有帮助,你不觉得吗?

  1. public static class Lookups
  2. {
  3. private static Dictionary<string,Vers> Versions;
  4.  
  5. static Lookups()
  6. {
  7. Versions = new Dictionary<string,Vers>
  8. {
  9. {"0000",new Vers {VersionNumber = "0000",VersionLiteral = "Location 1"}},{"0001",new Vers {VersionNumber = "0001",VersionLiteral = "Location 2"}},{"0002",new Vers {VersionNumber = "0002",VersionLiteral = "Location 3"}},{"0003",new Vers {VersionNumber = "0003",VersionLiteral = "Location 4"}},{"0004",new Vers {VersionNumber = "0004",VersionLiteral = "Location 5"}},{"0005",new Vers {VersionNumber = "0005",VersionLiteral = "Location 6"}},{"0006",new Vers {VersionNumber = "0006",VersionLiteral = "Location 7"}},{"0007",new Vers {VersionNumber = "0007",VersionLiteral = "Location 8"}}
  10. };
  11. }
  12.  
  13. public static bool VersionExists(string versionNumber)
  14. {
  15. return Versions.ContainsKey(versionNumber);
  16. }
  17.  
  18. public static string GetVersion(string s)
  19. {
  20. if (string.IsNullOrWhiteSpace(s))
  21. throw new FormatException("Empty version number!");
  22. return Versions[s.Trim()].VersionLiteral;
  23. }
  24. }

猜你在找的C#相关文章