Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the C # generic Dictionary Dictionary

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how to use the C# generic dictionary Dictionary, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use the C# generic dictionary Dictionary. Let's take a look.

The most common use of generics is generic collections. The namespace System.Collections.Generic contains some generic-based collection classes. Using generic collection classes can provide higher type safety and higher performance, avoiding repeated boxing and unboxing of non-generic collections.

Many non-generic collection classes have corresponding generic collection classes. I think it's best to get into the habit of using generic collection classes, which is not only good in performance but also more functional than non-generic classes. Here are the commonly used non-generic collection classes and their corresponding generic collection classes

Non-generic set class ArrayListListHashTableDIctionaryQueueQueueStackStackSortedListSortedList

Most of the non-generic collection classes we use are ArrayList class and HashTable class. When we often manipulate data information, we often use HashTable to store the information to be written to the database or return. In between, we have to constantly convert types. His help should be very great. If the data types we manipulate are relatively certain, it will be much more convenient to use Dictionary collection classes to store data. For example, when we need to store the user's shopping cart information (product name, corresponding number of items) in the e-commerce website, we can use Dictionary to store the shopping cart information without any type conversion.

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace L_Dictionary {class Program {static void printDict (Dictionary dict) {if (dict.Count = = 0) {Console.WriteLine ("--No data"); return } else {Console.WriteLine ("- print data");} foreach (KeyValuePair item in dict) {Console.WriteLine ("Key:" + item.Key + "Value:" + item.Value) }} static void Main (string [] args) {Console.WriteLine ("basic use of Dictionary") # region essence / / generics with Hashtable / / storing dictionaries in the form of key and value pairs (keys cannot be repeated) # endregion # region statement / / need to reference namespace / / using System.Collections.Generic; Dictionary dict = new Dictionary () # endregion # region add Console.WriteLine ("- increase"); / / key values cannot be repeated dict.Add (1,123 "); dict.Add (2,234"); dict.Add (3,345 ") / / dict.Add (3, "345"); / / error printDict (dict); # endregion # region delete Console.WriteLine ("--delete") / / you can only delete / / delete keys that do not exist through keys, and there is no response. Console.WriteLine ("delete key-1"); dict.Remove (1); Console.WriteLine ("delete key-4"); dict.Remove (4); printDict (dict); / / clear Console.WriteLine ("clear -"); dict.Clear (); printDict (dict) Dict.Add (1,123); dict.Add (2,234); dict.Add (3,345); # endregion # region query Console.WriteLine ("- query"); / / 1. Find the corresponding value through the key / / if the key does not have an error! Console.WriteLine ("query key 2:" + dict [2]); / Console.WriteLine (dict [4]); / / error Console.WriteLine ("query key 1:" + dict [1]); / / 2. Query whether there is / / query if (dict.ContainsKey (1)) {Console.WriteLine ("query exists elements with key value 1") } / / detect if based on value (dict.ContainsValue ("345")) {Console.WriteLine ("query exists elements with\" 345\ "value") } # endregion # region change Console.WriteLine ("- change"); Console.WriteLine ("modify key = 1 element value =\" 666\ "); dict [1] =" 666 "; printDict (dict) # endregion # region traverses Console.WriteLine ("- traversal"); / / 1. Traverse all keys Console.WriteLine ("- traversal keys"); foreach (int item in dict.Keys) {Console.WriteLine (item);} / / 2. Traversing all values Console.WriteLine ("- traversing all values"); foreach (string item in dict.Values) {Console.WriteLine (item);} / / 3. Traversing all keys Console.WriteLine ("- traversing all keys"); foreach (KeyValuePair item in dict) {Console.WriteLine ("Key:" + item.Key + "Value:" + item.Value);} # endregion Console.ReadLine ();}

This is the end of the article on "how to use the C# generic Dictionary Dictionary". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use the C# generic Dictionary Dictionary". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report