C# 泛型字典 Dictionary的使用详解
作者:廷益--飞鸟 时间:2022-01-19 23:48:17
本文主要介绍了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
标签:C#,泛型字典,Dictionary
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot中打war包需要注意事项
2023-11-05 19:44:42
![](https://img.aspxhome.com/file/2023/6/58616_0s.png)
JavaBean和SpringBean的区别及创建SpringBean方式
2022-05-23 03:32:14
Java桥梁设计模式优雅地将抽象与实现分离
2023-12-11 14:56:36
![](https://img.aspxhome.com/file/2023/9/97929_0s.png)
java中for和forEach的速度比较实例Demo
2022-06-20 07:09:58
Java中简单实用Quartz概述
2021-09-09 14:16:30
C#中Equals和GetHashCode使用及区别
2023-12-10 14:47:27
Java倒计时三种实现方式代码实例
2021-09-22 00:20:59
![](https://img.aspxhome.com/file/2023/7/91137_0s.png)
java实现简单的扫雷小游戏
2022-09-14 19:23:24
![](https://img.aspxhome.com/file/2023/3/95953_0s.jpg)
在Java中避免NullPointerException的解决方案
2023-10-17 04:47:00
C#实现读取txt文件生成Word文档
2022-08-06 19:03:22
![](https://img.aspxhome.com/file/2023/5/98215_0s.jpg)
Java String 拼接字符串原理详解
2023-05-14 10:10:33
IDEA编译乱码Build Output提示信息乱码
2023-08-07 12:14:35
![](https://img.aspxhome.com/file/2023/9/72369_0s.png)
java自动生成ID号的方法
2023-11-18 11:17:56
Java集合之Comparable和Comparator接口详解
2022-10-04 06:03:44
java开发分布式服务框架Dubbo原理机制详解
2023-01-04 19:53:01
![](https://img.aspxhome.com/file/2023/0/71210_0s.png)
5种Android数据存储方式汇总
2023-08-06 06:49:04
Java身份证验证方法实例详解
2023-04-04 02:22:11
java中的文件操作总结(干货)
2023-11-08 22:24:29
JWT在OpenFeign调用中进行令牌中继详解
2023-02-07 04:19:15
Java二维数组实战案例
2022-08-13 08:59:25
![](https://img.aspxhome.com/file/2023/6/63996_0s.png)