日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#?泛型字典?Dictionary的使用詳解_C#教程

作者:廷益--飛鳥 ? 更新時間: 2022-07-15 編程語言

本文主要介紹了C# 泛型字典 Dictionary的使用詳解,分享給大家,具體如下:

泛型最常見的用途是泛型集合,命名空間System.Collections.Generic 中包含了一些基于泛型的集合類,使用泛型集合類可以提供更高的類型安全性,還有更高的性能,避免了非泛型集合的重復的裝箱和拆箱。

很多非泛型集合類都有對應的泛型集合類,我覺得最好還是養成用泛型集合類的好習慣,他不但性能上好而且 功能上要比非泛型類更齊全。下面是常用的非泛型集合類以及對應的泛型集合類

非泛型集合類 泛型集合類
ArrayList List<T>
HashTable DIctionary<T>
Queue Queue<T>
Stack Stack<T>
SortedList SortedList<T>

我們用的比較多的非泛型集合類主要有? ArrayList類 和 HashTable類,其中當我們經常性的操作 數據信息時往往用HashTable 來存儲將要寫入到數據庫或者返回的信息,在這之間要不斷的進行類型的轉化,他給我們的幫助應該是非常大的,如果我們操縱的數據類型相對確定的化? 用Dictionary<TKey,TValue>集合類來存儲數據就方便多了,例如我們需要在電子商務網站中存儲用戶的購物車信息( 商品名,對應的商品個數)時,完全可以用Dictionary<string,int > 來存儲購物車信息,而不需要任何的類型轉化。

在這里插入圖片描述

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<int, string> dict) 
        {
            if(dict.Count == 0)
            {
                Console.WriteLine("--沒有數據");
                return;
            }
            else
            {
                Console.WriteLine("--打印數據");
            }

            foreach (KeyValuePair<int, string> item in dict)
            {
                Console.WriteLine("Key: " + item.Key + "  Value: " + item.Value);
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Dictionary 的基本使用");

            #region  本質
            // 擁有Hashtable的泛型
            // 以鍵、值對的方式存儲   字典(鍵不能重復)

            #endregion

            #region  申明
            // 需要引用命名空間
            // using System.Collections.Generic;

            Dictionary<int, string> dict = new Dictionary<int, string>();
            #endregion

            #region  增
            Console.WriteLine("-----------------------增");
            // 鍵值 不能重復
            dict.Add(1, "123");
            dict.Add(2, "234");
            dict.Add(3, "345");
            //dict.Add(3, "345");  // 報錯
            printDict(dict);
            #endregion

            #region  刪除
            Console.WriteLine("-----------------------刪除");
            // 只能通過鍵去刪除
            // 刪除不存在的 鍵, 沒有反應。
            Console.WriteLine("刪除鍵 --- 1");
            dict.Remove(1);
            Console.WriteLine("刪除鍵 --- 4");
            dict.Remove(4);
            printDict(dict);

            // 清空
            Console.WriteLine("清空 ----");
            dict.Clear();
            printDict(dict);

            dict.Add(1, "123");
            dict.Add(2, "234");
            dict.Add(3, "345");
            #endregion

            #region  查詢
            Console.WriteLine("-----------------------查詢");
            // 1.通過鍵找到 對應的值 
            //  如果鍵不存在 報錯!
            Console.WriteLine("查詢鍵2: " + dict[2]);
            // Console.WriteLine(dict[4]); // 報錯
            Console.WriteLine("查詢鍵1: " + dict[1]);

            // 2.查詢是否存在
            //    根據鍵查詢
            if (dict.ContainsKey(1))
            {
                Console.WriteLine("查詢 存在鍵值 1的元素");
            }
            // 根據值檢測
            if (dict.ContainsValue("345"))
            {
                Console.WriteLine("查詢 存在\"345 \"值的元素");
            }

            #endregion

            #region  改
            Console.WriteLine("-----------------------改");
            Console.WriteLine("修改 鍵=1 元素 值= \"666\" ");
            dict[1] = "666";
            printDict(dict);

            #endregion

            #region  遍歷
            Console.WriteLine("-----------------------遍歷");
            // 1.遍歷所有鍵
            Console.WriteLine("---遍歷鍵");
            foreach (int item in dict.Keys)
            {
                Console.WriteLine(item);
            }

            // 2.遍歷所有值
            Console.WriteLine("---遍歷所有值");
            foreach (string item in dict.Values)
            {
                Console.WriteLine(item);
            }
            // 3.遍歷所有鍵值
            Console.WriteLine("---遍歷所有鍵值");
            foreach (KeyValuePair<int, string> item in dict)
            {
                Console.WriteLine("Key: " + item.Key + "  Value: " + item.Value);
            }
            #endregion

            Console.ReadLine();
        }
    }
}

在這里插入圖片描述

原文鏈接:https://blog.csdn.net/weixin_45875105/article/details/124789800

欄目分類
最近更新