欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 泛型字典 Dictionary的使用詳解

 更新時(shí)間:2022年05月20日 09:49:54   作者:廷益--飛鳥  
本文主要介紹了C# 泛型字典 Dictionary的使用詳解,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

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

非泛型集合類泛型集合類
ArrayListList<T>
HashTableDIctionary<T>
QueueQueue<T>
StackStack<T>
SortedListSortedList<T>

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

在這里插入圖片描述

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("--沒有數(shù)據(jù)");
                return;
            }
            else
            {
                Console.WriteLine("--打印數(shù)據(jù)");
            }

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

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

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

            #endregion

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

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

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

            #region  刪除
            Console.WriteLine("-----------------------刪除");
            // 只能通過鍵去刪除
            // 刪除不存在的 鍵, 沒有反應(yīng)。
            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.通過鍵找到 對(duì)應(yīng)的值 
            //  如果鍵不存在 報(bào)錯(cuò)!
            Console.WriteLine("查詢鍵2: " + dict[2]);
            // Console.WriteLine(dict[4]); // 報(bào)錯(cuò)
            Console.WriteLine("查詢鍵1: " + dict[1]);

            // 2.查詢是否存在
            //    根據(jù)鍵查詢
            if (dict.ContainsKey(1))
            {
                Console.WriteLine("查詢 存在鍵值 1的元素");
            }
            // 根據(jù)值檢測(cè)
            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();
        }
    }
}

在這里插入圖片描述

到此這篇關(guān)于C# 泛型字典 Dictionary的使用詳解的文章就介紹到這了,更多相關(guān)C# 泛型字典 Dictionary內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論