C#学习相关系列之数据类型---Diactionary字典的用法

本文详细介绍了C#中的Dictionary类,包括其创建、操作(添加、删除、访问)、排序(OrderBy,OrderByDescending)、集合运算(Intersect、Except、Union)以及查找键值对的方法。
摘要由CSDN通过智能技术生成

一、字典的简介和常用方法

       在C#中,Dictionary<TKey, TValue>是一种非常常用的泛型集合类,用于存储键值对(Key-Value Pair)的数据结构。Dictionary<TKey, TValue>可以根据键快速查找对应的值,因此在需要快速查找和检索数据的场景下,特别是在涉及大量数据时,使用字典是非常高效的选择。本文将详细介绍Dictionary<TKey, TValue>的应用,包括创建字典、添加元素、访问元素、删除元素、遍历字典、常用的方法等内容。
常用方法:

Comparer:           获取用于确定字典中的键是否相等的 IEqualityComparer

  Count:                  获取包含在 Dictionary中的键/值对的数目。

  Item:                    获取或设置与指定的键相关联的值。

  Keys:                   获取包含 Dictionary中的键的集合。

  Values:                获取包含 Dictionary中的值的集合。

  Add:                    将指定的键和值添加到字典中。

  Clear:                  从 Dictionary中移除所有的键和值。

  ContainsKey:      确定 Dictionary是否包含指定的键。

  ContainsValue:   确定 Dictionary是否包含特定值。             

  GetEnumerator:  返回循环访问 Dictionary的枚举数。

  GetType:             获取当前实例的 Type。 (从 Object 继承。)

  Remove:             从 Dictionary中移除所指定的键的值。

  ToString:             返回表示当前 Object的 String。 (从 Object 继承。)

  TryGetValue:      获取与指定的键相关联的值。

二、主要方法方法简介

1、排序orderby

排序主要为正序orderby和反序OrderByDescending,二者用法相同

        public static Dictionary<int, string> dic = new Dictionary<int, string>();  
        static void Main(string[] args)
        {
            dic.Add(1,"a");
            dic.Add(3, "c");
            dic.Add(6, "b");
            dic.Add(2, "f");
            第一种方法:
            var tt = dic.OrderBy(x => x.Value);
            //orderby后面跟要排序的依据x为字典,通过x的value进行正向排序
            第二种方法:
           // var tt = from s in dic orderby s.Value descending select s;
            foreach (var item in tt)
            {
                Console.WriteLine(item.Key+"  "+item.Value);
            }

dictionary的排序主要用orderby方法,具体可以参考linq中orderby的用法。

2、对Dictionary求交集Intersect、差集Except、并集Union并集

Dictionary<int, int> Dic1 = new Dictionary<int, int>();
for (int i = 0; i < 10; i++)
{
  Dic1.Add(i, i);
}
 
Dictionary<int, int> Dic2 = new Dictionary<int, int>();
for (int i = 5; i < 15; i++)
{
  Dic2.Add(i, i);
}
 
//求交集
 
var jj = Dic1.Keys.Intersect(Dic2.Keys);
foreach (var i in jj)
{
   Console.Write(i + " ");
}
 
//求差集
var cj = Dic1.Keys.Except(Dic2.Keys);
 
foreach (var i in cj)
{
   Console.Write(i + " ");
}
 
//求并集
var bj = Dic1.Keys.Union(Dic2.Keys);
 
foreach (var i in bj)
{
   Console.Write(i + " ");
}

3、通过值查找键

Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("apple", 1);
dict.Add("banana", 2);
dict.Add("orange", 3);

string key = dict.FirstOrDefault(x => x.Value == 2).Key;
//FirstOrDefault中跟的是判断条件
Console.WriteLine(key); // 输出 "banana"

4.判断两个字典是否相等

        bool Equals(Dictionary<string, int> dict1, Dictionary<string, int> dict2)
        {
            var dict3 = dict2.Where(x => !dict1.ContainsKey(x.Key) || dict1[x.Key] != x.Value)
                             .Union(dict1.Where(x => !dict2.ContainsKey(x.Key) || dict2[x.Key] != x.Value))
                             .ToDictionary(x => x.Key, x => x.Value);
            return dict3.Count == 0;
        }

基础方法的使用:

【C# 基础精讲】字典(Dictionary)的使用_c# dictionary_繁依Fanyi的博客-CSDN博客

C#“字典”Dictionary的用法_c# dictionary_mr_five55的博客-CSDN博客

参考文献:

C#系 常用的LinQ查询表达式之orderby_yy763496668的博客-CSDN博客

C# 判断两个字典是否相等_c#2个字典 对比 方法-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值