哪种将项添加到 ASP.NET 字典类的方法更有效
本文关键字:字典 方法 有效 NET ASP 添加 | 更新日期: 2023-09-27 18:30:38
我正在使用 C# 将逗号分隔的字符串列表转换为字典 ASP.NET 中的(通过省略任何重复项):
string str = "1,2, 4, 2, 4, item 3,item2, item 3"; //Just a random string for the sake of this example
我想知道哪种方法更有效?
1 - 使用 try/catch 块:
Dictionary<string, string> dic = new Dictionary<string, string>();
string[] strs = str.Split(',');
foreach (string s in strs)
{
if (!string.IsNullOrWhiteSpace(s))
{
try
{
string s2 = s.Trim();
dic.Add(s2, s2);
}
catch
{
}
}
}
2 - 或使用 ContainsKey() 方法:
string[] strs = str.Split(',');
foreach (string s in strs)
{
if (!string.IsNullOrWhiteSpace(s))
{
string s2 = s.Trim();
if (!dic.ContainsKey(s2))
dic.Add(s2, s2);
}
}
谢谢所有参与的人!
一个非常有趣的发现。如果你看看下面dtb提供的答案,他提出了两种使用hashSet的方法。我将在这里为它们配音:
方法一:
var hashSet = new HashSet<string>(from s in str.Split(',')
where !string.IsNullOrWhiteSpace(s)
select s.Trim());
方法2:
var hashSet = new HashSet<string>();
foreach (string s in str.Split(','))
{
if (!string.IsNullOrWhiteSpace(s))
{
hashSet.Add(s.Trim());
}
}
我问他哪种方法在性能方面更快,有趣的是,方法2更快。下面是使用 Stopwatch 类完成的计时,方法是在循环中运行发布版本中的每个方法 1,000,000 次:
Method 1: 1,440 ms average
Method 2: 1,124 ms average
如果你需要一个集合而不是字典,我建议你使用 HashSet
<小时 />HashSet
类 表示一组值。
集合是不包含重复元素的集合,其元素没有特定的顺序。
var hashSet = new HashSet<string>(from s in str.Split(',')
where !string.IsNullOrWhiteSpace(s)
select s.Trim());
或同等
var hashSet = new HashSet<string>();
foreach (string s in str.Split(','))
{
if (!string.IsNullOrWhiteSpace(s))
{
hashSet.Add(s.Trim());
}
}
看起来
不需要字典:一个简单的 LINQ 表达式应该为您提供一个没有重复项的列表:
var res = str
.Split(',')
.Where(s => !string.IsNullOrWhitespace(s))
.Select(s => s.Trim())
.Distinct()
.ToList();
如果你坚持要字典,你可以改用ToDictionary
:
var res = str
.Split(',')
.Where(s => !string.IsNullOrWhitespace(s))
.Select(s => s.Trim())
.Distinct()
.ToDictionary(s=>s, s=>s);
强烈建议不要在正常程序流中使用try/catch
,因为它隐藏了您的意图:C# 中的异常是为特殊情况保留的,而不是您可以使用if / then / else
条件安全地捕获的常规内容。
方法
2 使用 .ContainsKey
,比抑制异常更具语义性,并且很可能更有效。
使用异常来控制预期的执行流通常是不受欢迎的,并且捕获异常的成本很高,所以我选择了 v2。如果它对你来说真的很重要,为什么不建立一个基准呢?我猜 2 "更有效",但你可以很容易地确认这一点。
如果您没有使用字典中的值,则可以改用HashSet<string>
,将项目添加到哈希集会自动删除重复项:
HashSet<string> set = new HashSet<string>(
str.Split(',')
.Select(s => s.Trim())
.Where(s => s.Length > 0)
);