当前位置: 首页 > news >正文

C#中的字典怎么使用?

在C#中,Dictionary<TKey, TValue> 是一个泛型集合类,用于存储键值对(key-value pairs)。它提供了快速的查找、插入和删除操作,适合需要根据键快速查找值的场景。以下是 Dictionary 的基本用法和常见操作:

1. 创建字典

使用 Dictionary<TKey, TValue> 类创建字典,其中 TKey 是键的类型,TValue 是值的类型。

// 创建一个键为 string,值为 int 的字典
Dictionary<string, int> ages = new Dictionary<string, int>();

2. 添加键值对

使用 Add 方法或索引器向字典中添加键值对。

// 使用 Add 方法添加
ages.Add("Alice", 30);
ages.Add("Bob", 25);// 使用索引器添加(如果键已存在,会覆盖值)
ages["Charlie"] = 35;

3. 访问值

通过键访问字典中的值。

// 使用索引器访问
int aliceAge = ages["Alice"];
Console.WriteLine($"Alice's age: {aliceAge}");// 使用 TryGetValue 安全访问(避免键不存在时抛出异常)
if (ages.TryGetValue("Bob", out int bobAge))
{Console.WriteLine($"Bob's age: {bobAge}");
}
else
{Console.WriteLine("Bob's age not found.");
}

4. 修改值

通过键修改字典中的值。

// 修改 Alice 的年龄
ages["Alice"] = 31;
Console.WriteLine($"Alice's new age: {ages["Alice"]}");

5. 删除键值对

使用 Remove 方法删除指定键的键值对。

// 删除键为 "Bob" 的键值对
ages.Remove("Bob");// 检查是否删除成功
if (!ages.ContainsKey("Bob"))
{Console.WriteLine("Bob's age has been removed.");
}

6. 检查键或值是否存在

使用 ContainsKey 或 ContainsValue 方法检查字典中是否包含指定的键或值。

// 检查键是否存在
if (ages.ContainsKey("Alice"))
{Console.WriteLine("Alice is in the dictionary.");
}// 检查值是否存在
if (ages.ContainsValue(35))
{Console.WriteLine("Someone is 35 years old.");
}

7. 遍历字典

使用 foreach 循环遍历字典中的键值对。

foreach (var kvp in ages)
{Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}// 单独遍历键或值
foreach (var key in ages.Keys)
{Console.WriteLine($"Key: {key}");
}foreach (var value in ages.Values)
{Console.WriteLine($"Value: {value}");
}

8. 清空字典

使用 Clear 方法清空字典中的所有键值对。

ages.Clear();
Console.WriteLine($"Dictionary count after clearing: {ages.Count}");

9. 获取字典的大小

使用 Count 属性获取字典中键值对的数量。

Console.WriteLine($"Number of entries in the dictionary: {ages.Count}");

10. 初始化字典

可以在创建字典时直接初始化键值对。

Dictionary<string, int> ages = new Dictionary<string, int>
{{ "Alice", 30 },{ "Bob", 25 },{ "Charlie", 35 }
};

11. 处理键冲突

如果尝试添加一个已经存在的键,Add 方法会抛出 ArgumentException。可以使用 ContainsKey 方法检查键是否存在,或者使用索引器直接赋值。

if (!ages.ContainsKey("Alice"))
{ages.Add("Alice", 30);
}
else
{Console.WriteLine("Alice already exists in the dictionary.");
}

12. 字典的默认值

如果尝试访问不存在的键,索引器会抛出 KeyNotFoundException。可以使用 TryGetValue 方法避免异常。

if (ages.TryGetValue("David", out int davidAge))
{Console.WriteLine($"David's age: {davidAge}");
}
else
{Console.WriteLine("David's age not found.");
}

13. 使用自定义类型作为键

如果使用自定义类型作为键,需要确保该类型正确实现了 Equals 和 GetHashCode 方法,以便字典能够正确比较和查找键。

public class Person
{public string Name { get; set; }public int Age { get; set; }public override bool Equals(object obj){if (obj is Person other){return Name == other.Name && Age == other.Age;}return false;}public override int GetHashCode(){return Name.GetHashCode() ^ Age.GetHashCode();}
}// 使用自定义类型作为键
Dictionary<Person, string> personDescriptions = new Dictionary<Person, string>
{{ new Person { Name = "Alice", Age = 30 }, "Software Engineer" },{ new Person { Name = "Bob", Age = 25 }, "Data Scientist" }
};

14. 字典的性能

  • 查找Dictionary 的查找操作是 O(1) 时间复杂度,因为它是基于哈希表实现的。

  • 插入和删除:插入和删除操作的平均时间复杂度也是 O(1)。

  • 内存开销:由于哈希表的实现,Dictionary 会占用较多的内存。

15. 完整示例

以下是一个完整的示例,展示了字典的常见操作:

using System;
using System.Collections.Generic;class Program
{static void Main(){// 创建并初始化字典Dictionary<string, int> ages = new Dictionary<string, int>{{ "Alice", 30 },{ "Bob", 25 },{ "Charlie", 35 }};// 添加新键值对ages["David"] = 28;// 修改值ages["Alice"] = 31;// 删除键值对ages.Remove("Bob");// 遍历字典foreach (var kvp in ages){Console.WriteLine($"{kvp.Key}: {kvp.Value}");}// 检查键是否存在if (ages.ContainsKey("Charlie")){Console.WriteLine("Charlie is in the dictionary.");}// 获取字典大小Console.WriteLine($"Number of entries: {ages.Count}");}
}

http://www.lryc.cn/news/544659.html

相关文章:

  • vue框架后遗症∶被遗忘的dom操作
  • 进程 ─── linux第10课
  • 线性模型 - 支持向量机
  • MyBatis-Plus注解配置:@TableName、@TableId、@TableField
  • DeepSeek接入问题-Xshell5连接Ubuntu22失败解决方案
  • 论文阅读之基于Syn2Real域的侧扫声纳类水雷目标探测
  • 【Java】Tomcat日志
  • datalist 是什么?
  • 初阶数据结构(C语言实现)——3顺序表和链表(3)
  • Docker 数据卷管理及优化
  • Hi3516CV610车牌识别算法源码之——车牌识别算法初体验
  • 使用内置命令查看笔记本电池健康状态
  • HONOR荣耀MagicBook 15 2021款 独显(BOD-WXX9,BDR-WFH9HN)原厂Win10系统
  • transformer架构的语言模型保存的内容与格式详解
  • win本地vscode通过代理远程链接linux服务器
  • 【C++编程探索】01前缀和来临!优点多多!八千字详解
  • 文件下载技术的终极选择:`<a>` 标签 vs File Saver.js
  • 《机器学习数学基础》补充资料:矩阵的LU分解
  • [笔记.AI]AI知识科普提纲
  • Spring Security 如何防止 CSRF 攻击?
  • 使用 Kubeflow 和 Ray 构建机器学习平台
  • SEO炼金术(4)| Next.js SEO 全攻略
  • 每日十个计算机专有名词 (7)
  • StarRocks 在爱奇艺大数据场景的实践
  • 蓝桥杯好题推荐----高精度乘法
  • Linux网络 数据链路层
  • 量子计算可能改变世界的四种方式
  • React 组件基础介绍
  • ETL系列-数据抽取(Extract)
  • java八股文之框架