这些值可以达到数百个.由于95%的客户在没有Internet访问权限的计算机上安装我的应用程序,因此我必须假设我的应用程序无法访问Internet或访问数据库.
所以我想知道这是否是一种有效的处理方法,如果我在方法完成后正确处理对象:
代码:
- using System;
- using System.Collections.Generic;
- namespace FunctionLibrary
- {
- public static class Lookups
- {
- private static List<Vers> Versions;
- public static string GetVersion(string s)
- {
- string retValue = string.Empty;
- Versions = new List<Vers>();
- try
- {
- if (s.Trim().Length > 0)
- {
- GetVersions();
- retValue = Versions.Find(ver => ver.VersionNumber == s).VersionLiteral;
- if (string.IsNullOrEmpty(retValue))
- {
- retValue = string.Format("{0} is an Unknown Version Number",s);
- }
- }
- else
- {
- retValue = "No version number supplied";
- }
- }
- catch
- {
- retValue = string.Format("{0} is an Unknown Version Number",s);
- }
- finally
- {
- Versions.Clear();
- Versions = null;
- }
- return retValue;
- }
- private static void GetVersions()
- {
- Versions.Add(new Vers() { VersionNumber = "0000",VersionLiteral = "Location 1" });
- Versions.Add(new Vers() { VersionNumber = "0001",VersionLiteral = "Location 2" });
- Versions.Add(new Vers() { VersionNumber = "0002",VersionLiteral = "Location 3"});
- Versions.Add(new Vers() { VersionNumber = "0003",VersionLiteral = "Location 4"});
- Versions.Add(new Vers() { VersionNumber = "0004",VersionLiteral = "Location 5"});
- Versions.Add(new Vers() { VersionNumber = "0005",VersionLiteral = "Location 6"});
- Versions.Add(new Vers() { VersionNumber = "0006",VersionLiteral = "Location 7"});
- Versions.Add(new Vers() { VersionNumber = "0007",VersionLiteral = "Location 8"});
- }
- }
- public class Vers
- {
- public string VersionLiteral { get; set; }
- public string VersionNumber { get; set; }
- }
- }
解决方法
关于List<T>
vs Dictionary<TKey,TValue>
vs Lookup<TKey,TElement>
的一些一般性说明
正如其他答案所示,在您的场景中使用List非常糟糕,主要是因为查找元素会有不良表现.
在Dictionary和Lookup之间选择并不难(从MSDN开始,强调我的):
A
Lookup<TKey,TElement>
resembles aDictionary<TKey,TValue>.
The difference is
that aDictionary<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 callingToLookup
on an object that implementsIEnumerable<T>.
由于您只需要将键映射到单个值,因此Dictionary是正确的选择.
previously accepted answer是朝着正确方向迈出的一步,但仍然会出现几个关键问题(编辑:这些问题已经解决).
字符串是不可变的:s.Trim()不会改变s – 它将返回一个新字符串,这意味着你需要s = s.Trim()如果你之后使用s,那就是你.
静态类不能有实例构造函数:public Lookups()应该是静态的Lookups()(当然不允许静态构造函数具有访问修饰符).
不要将空字符串/错误消息作为字符串返回!
这将成为一个奇妙的调试头痛.您应该使用异常而不是传递错误字符串 – 并且您应该提供VersionExists方法来检查您的字典是否包含特定版本!
修改,更安全的例子
如果参数为空,null或空格,则抛出FormatException.如果版本不存在,Dictionary会抛出一个KeyNotFoundException – 比string.Empty对调试更有帮助,你不觉得吗?
- public static class Lookups
- {
- private static Dictionary<string,Vers> Versions;
- static Lookups()
- {
- Versions = new Dictionary<string,Vers>
- {
- {"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"}}
- };
- }
- public static bool VersionExists(string versionNumber)
- {
- return Versions.ContainsKey(versionNumber);
- }
- public static string GetVersion(string s)
- {
- if (string.IsNullOrWhiteSpace(s))
- throw new FormatException("Empty version number!");
- return Versions[s.Trim()].VersionLiteral;
- }
- }