C# Dictionary.Add 与 Dictionary[key]=value 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11557418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference of Dictionary.Add vs Dictionary[key]=value
提问by rsg
What is the difference between the Dictionary.Addmethod and the indexer Dictionary[key] = value?
Dictionary.Add方法和索引器有Dictionary[key] = value什么区别?
采纳答案by Habib
Add-> Adds an item to the dictionary if item already exists in the dictionary an exception will be thrown.
添加-> 如果项目已存在于字典中,则将项目添加到字典中,将引发异常。
Indexer or Dictionary[Key]=> Add Or Update. If the key doesn't exist in the dictionary, a new item will be added. If the key exists then the value will be updated with the new value.
索引器或Dictionary[Key]=> 添加或更新。如果字典中不存在该键,则会添加一个新项目。如果键存在,则该值将使用新值更新。
dictionary.addwill add a new item to the dictionary, dictionary[key]=valuewill set a value to an existing entry in the dictionary against a key. If the key is not present then it (indexer)will add the item in the dictionary.
dictionary.add将向字典添加新项目,dictionary[key]=value将值设置为字典中现有条目的键值。如果键不存在,那么它(索引器)将在字典中添加该项目。
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("Test", "Value1");
dict["OtherKey"] = "Value2"; //Adds a new element in dictionary
Console.Write(dict["OtherKey"]);
dict["OtherKey"] = "New Value"; // Modify the value of existing element to new value
Console.Write(dict["OtherKey"]);
In the above example, in first place dict["OtherKey"] = "Value2";will add a new value in the dictionary because it doesn't exist, and in second place it will modify the value to New Value.
在上面的例子中,首先dict["OtherKey"] = "Value2";将在字典中添加一个新值,因为它不存在,其次将值修改为新值。
回答by Jeeva
dictionary.addadd an item to the dictionary whereas dictionary[key]=valueassigns a value to an already existing key.
dictionary.add将一个项目添加到字典中,同时dictionary[key]=value为一个已经存在的键分配一个值。
回答by xanatos
Dictionary.Addthrows an exception if the key is already present. []when used for setting an item doesn't (it does if you try to access it for read).
Dictionary.Add如果键已经存在,则抛出异常。[]用于设置项目时不会(如果您尝试访问它以进行读取,则不会)。
x.Add(key, value); // will throw if key already exists or key is null
x[key] = value; // will throw only if key is null
var y = x[key]; // will throw if key doesn't exists or key is null
回答by cdhowie
The behavior is identical when the key does not exist in the dictionary: the item will be added in both cases.
当字典中不存在键时,行为是相同的:在这两种情况下都将添加该项目。
The behavior differs when the key already exists. dictionary[key] = valuewill update the value mapped to the key, while dictionary.Add(key, value)will instead throw an ArgumentException.
当密钥已经存在时,行为会有所不同。 dictionary[key] = value将更新映射到键的值,而dictionary.Add(key, value)将抛出 ArgumentException。
回答by Jon Skeet
The documentation for Addmakes this very clear, I feel:
的文档Add对此非常清楚,我觉得:
You can also use the
Itemproperty to add new elements by setting the value of a key that does not exist in theDictionary(Of TKey, TValue); for example,myCollection[myKey] = myValue(in Visual Basic,myCollection(myKey) = myValue). However, if the specified key already exists in theDictionary(Of TKey, TValue), setting the Item property overwrites the old value. In contrast, theAddmethod throws an exception if a value with the specified key already exists.
您还可以使用该
Item属性通过设置 中不存在的键的值来添加新元素Dictionary(Of TKey, TValue);例如,myCollection[myKey] = myValue(在 Visual Basic 中,myCollection(myKey) = myValue)。但是,如果指定的键已存在于 中Dictionary(Of TKey, TValue),则设置 Item 属性会覆盖旧值。相反,Add如果具有指定键的值已存在,则该方法将引发异常。
(Note that the Itemproperty corresponds to the indexer.)
(请注意,该Item属性对应于索引器。)
It's always worth consulting the documentation before asking a question...
在提出问题之前总是值得参考文档......

