软件编程
位置:首页>> 软件编程>> C#编程>> C# 泛型字典 Dictionary的使用详解

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

作者:廷益--飞鸟  发布时间:2022-01-19 23:48:17 

标签:C#,泛型字典,Dictionary

本文主要介绍了C# 泛型字典 Dictionary的使用详解,分享给大家,具体如下:

泛型最常见的用途是泛型集合,命名空间System.Collections.Generic 中包含了一些基于泛型的集合类,使用泛型集合类可以提供更高的类型安全性,还有更高的性能,避免了非泛型集合的重复的装箱和拆箱。

很多非泛型集合类都有对应的泛型集合类,我觉得最好还是养成用泛型集合类的好习惯,他不但性能上好而且 功能上要比非泛型类更齐全。下面是常用的非泛型集合类以及对应的泛型集合类

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

我们用的比较多的非泛型集合类主要有  ArrayList类 和 HashTable类,其中当我们经常性的操作 数据信息时往往用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,他给我们的帮助应该是非常大的,如果我们操纵的数据类型相对确定的化  用Dictionary<TKey,TValue>集合类来存储数据就方便多了,例如我们需要在电子商务网站中存储用户的购物车信息( 商品名,对应的商品个数)时,完全可以用Dictionary<string,int > 来存储购物车信息,而不需要任何的类型转化。

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

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();
       }
   }
}

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

来源:https://blog.csdn.net/weixin_45875105/article/details/124789800

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com