字典的行为 [英] Behaviour of Dictionary<>

查看:86
本文介绍了字典的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然HashSet的Add()方法在值已存在时不抱怨,但字典会抛出异常重复键".在许多情况下,开发人员使用的代码如下:

While the Add() method of a HashSet doesn't moan when a value exist already, a dictionary throws an exception "duplicate key". In many cases developer use code like this:

if (!myDict.ContainsKey("himalaya"))
{
       myDict.Add("himalaya", 1960);
}

有时需要与HashSet相同的行为:如果密钥已经存在,则只需忽略Add()即可.

Sometimes the same behaviour like that of a HashSet is needed: Just ignore the Add() if the key exists already.

在其他情况下,如果密钥已经存在,则开发人员需要更新该值.经常看到的代码如下:

In other cases, developers Need to update the value in case the key exists already. Code often seen looks like this:

if (myDict.ContainsKey("nanga parbat"))
{
    myDict["nanga parbat"] = 1934;
}
else
{
    myDict.Add("nanga parbat", 1934);
}

如果要更新已存在的键/值对的值,或者如果不存在,则添加键/值对,此代码很有用.

This is code useful if one wants to update the value of an already existing key/value pair or if absent, add the key/value pair.

但是,字典可以将此类代码封装在ist类中,并允许开发人员在上述szenarios情况下配置字典.

However the dictionary could encapsulate such code within ist class and allow developers to configure the dictionary in case of the above mentioned szenarios.

例如,如果您只想在键已经存在的情况下忽略添加",则可以这样配置字典,并且不会抛出任何异常:

For example, if you just want to ignore an Add in case the key exists already, the dictionary could be configured like this and does not throw any exception:

myDict.Behaviour = Behaviour.IgnoreAddOnExistingKey;

如果某个键存在时想要更新值,则可以按以下方式配置字典:

In case one wants to update the value if a key exists, the dictionary could be configured like this:

myDict.Behaviour = Behaviour.UpdateValueOnExistingKey;

如果尝试添加键/值对时键存在,则后者只会更新值.示例:

The latter would just update the value in case the key exists when trying to add a key/value pair. Example:

myDict.Add("himalaya", "1959");
myDict.Behaviour = Behaviour.UpdateValueOnExistingKey;
myDict.Add("himalaya", "1960");

// second add method call just updates value for key "himalaya"

枚举行为将具有以下枚举:

The enum Behaviour would have the following enums:

Behaviour.IgnoreAddOnExistingKey
Behaviour.UpdateValueOnExistingKey
Behaviour.Normal

枚举行为还可以在每次调用的基础上与重载的Add()方法一起使用.

The enum Behaviour could also be used with an overloaded Add() method on a per-call basis.

像这样,对于上面提到的szenario,不再需要ContainsKey()方法调用.

Like this, ContainsKey() method calls are no more neccessary for the above mentioned szenarios.

  

  

Peter

推荐答案

Peter,

我了解这是您要增强Dictionary对象的实现的建议. Web上有一个专门针对此类建议的网站,各个团队都在跟踪它.

请前往https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/category/30931-languages-c提交您的建议.

附带说明,这些论坛旨在以德语提供.如果您正在使用英语进行通用C#论坛,请尝试https://social.msdn.microsoft.com/Forums/en-US/home?forum=csharpgeneral

谢谢.
Peter,

I understand this is a suggestion you would like to make to enhance the implementation of the Dictionary object. There is a site on the web that is intended exactly for that kind of suggestions, and the respective teams do keep track of it.

Please head on over to https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/category/30931-languages-c to file your suggestion.

As a sidenote, these forums are intended to be in German. If you're after a general C# forum in English language, try https://social.msdn.microsoft.com/Forums/en-US/home?forum=csharpgeneral

Thanks.


这篇关于字典的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆