English 中文(简体)
给伙伴关系增加物品的方法。 NET Dictionary category is more effective?
原标题:Which method of adding items to the ASP.NET Dictionary class is more efficient?

I m converting a comma separated list of strings into a dictionary using C# in ASP.NET (by omitting any duplicates):

string str = "1,2, 4, 2, 4, item 3,item2, item 3"; //Just a random string for the sake of this example

我不知道哪一种方法更有效率?

1 - 使用审判/渔获区:

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- 采用Key()方法:

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);
    }
}

<>strong>EDIT. 感谢大家参加!

令人非常感兴趣的发现。 如果你看一下下文中提供的答案,他就建议采用两种办法。 我在此指责他们:

方法1:

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能够更快。 这里使用的是“停止监视”类别,在释放中操作每一种方法,在 lo中建造1 000 000次:

方法1: 1,440 ms average
方法2: 1,124 ms average
最佳回答

如果你需要一套而不是字典,我建议你使用。 HashSet<T> Level:

HashSet<T> Class

代表一套价值观。

一组是没有重复内容的收集,其内容并不具体。


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();

如果您坚持使用字典,则您可使用<条码>。

var res = str
    .Split( , )
    .Where(s => !string.IsNullOrWhitespace(s))
    .Select(s => s.Trim())
    .Distinct()
    .ToDictionary(s=>s, s=>s);

在正常方案流动中使用<条码>/海床/代码>是强烈劝阻的,因为它掩盖了你的意图:C#中的例外情况是保留给非常的情况,而不是你能够安全锁定<条码>的常规物品。

Method 2, using .ContainsKey, is more semantic and, most likely, more efficient than suppressing the exception.

使用例外情况控制预期的执行流动通常会被搁置起来,而追捕例外是昂贵的,因此我选择不适用。 如果这确实对你很重要,为什么没有制定基准? 我猜想2“效率更高”,但你可以轻易证实这一点。

If you don t have any use of the values in the dictionary, you can use a HashSet<string> instead, and adding items to a hash set automatically removes duplicates:

HashSet<string> set = new HashSet<string>(
  str.Split( , )
  .Select(s => s.Trim())
  .Where(s => s.Length > 0)
);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签