C # dictionary. Add (key, "123") and dictionary [Key] = "123"

Source: Internet
Author: User

1. Description of dictionary [Key] = "123" on msdn.

Http://msdn.microsoft.com/zh-cn/library/9tee9ht2 (V = vs.85). aspx

This attribute provides the ability to access specific elements in a collection using the following C # Syntax: mycollection [Key] (mycollection (key in Visual Basic )).

You can use the item attribute to add new elements by setting a key value that does not exist in the dictionary. When setting the attribute value, if the key is in a dictionary, the assigned value replaces the value associated with the key. If the key is not in the dictionary, the key and value are added to the dictionary. In contrast, the add method does not modify existing elements.

The key cannot be a null reference, but if the value type tvalue is a reference type, this value can be null.

 

2. The link of dictionary. Add (key, "123") on msdn.

Http://msdn.microsoft.com/zh-cn/library/k7z0zy8k (V = vs.85). aspx

 

3. Summary (dictionary. Add (key, "123") and dictionary [Key] = "123 ")

If the key already exists in the dictionary, the system returns the error "add": The dictionary has an element with the same key.
Dictionary [Key] does not report an error. If it is not added, it is changed or replaced. Therefore, it is recommended to use this method, which is safer without having to write an if statement.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.