千家信息网

C#泛型字典Dictionary怎么使用

发表于:2025-02-22 作者:千家信息网编辑
千家信息网最后更新 2025年02月22日,这篇文章主要介绍了C#泛型字典Dictionary怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C#泛型字典Dictionary怎么使用文章都会有所收获,下面我们
千家信息网最后更新 2025年02月22日C#泛型字典Dictionary怎么使用

这篇文章主要介绍了C#泛型字典Dictionary怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C#泛型字典Dictionary怎么使用文章都会有所收获,下面我们一起来看看吧。

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

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

非泛型集合类泛型集合类
ArrayListList
HashTableDIctionary
QueueQueue
StackStack
SortedListSortedList

我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类,其中当我们经常性的操作 数据信息时往往用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,他给我们的帮助应该是非常大的,如果我们操纵的数据类型相对确定的化 用Dictionary集合类来存储数据就方便多了,例如我们需要在电子商务网站中存储用户的购物车信息( 商品名,对应的商品个数)时,完全可以用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 dict)         {            if(dict.Count == 0)            {                Console.WriteLine("--没有数据");                return;            }            else            {                Console.WriteLine("--打印数据");            }            foreach (KeyValuePair 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 dict = new Dictionary();            #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 item in dict)            {                Console.WriteLine("Key: " + item.Key + "  Value: " + item.Value);            }            #endregion            Console.ReadLine();        }    }}

关于"C#泛型字典Dictionary怎么使用"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"C#泛型字典Dictionary怎么使用"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。

0