Python 中字典 Dictionary 详解

开发 前端
Dictionary<TKey, TValue>​是C#中用于存储键值对集合的泛型类,属于System.Collections.Generic​命名空间。它允许使用键(Key​)来访问与其关联的值(Value)。其中,TKey表示字典中键的类型,TValue表示字典中值的类型。

基本概念

Dictionary<TKey, TValue>是C#中用于存储键值对集合的泛型类,属于System.Collections.Generic命名空间。它允许使用键(Key)来访问与其关联的值(Value)。其中,TKey表示字典中键的类型,TValue表示字典中值的类型。

Dictionary的基本结构

  • 键(Key):唯一标识集合中的一个元素。键是唯一的,不能有重复。
  • 值(Value):与键相关联的数据。值可以是任意类型,并且可以有重复。
  • 键值对(KeyValuePair):键和值的组合,表示Dictionary中的一个元素。

Dictionary的主要特性

  • 快速访问:通过键可以快速检索到对应的值,平均时间复杂度接近O(1),因为Dictionary<TKey,TValue>类是作为哈希表实现。
  • 唯一键(Key):每个键在Dictionary中都是唯一的,不能重复。
  • 动态大小:Dictionary的大小可以动态调整,当元素数量超过容量时,它会自动扩容。
  • 无序集合:Dictionary中的元素是无序的,不能通过索引来访问它们。

Dictionary的常用操作

以下是C#中Dictionary的常用操作完整代码,其中包括添加元素、访问元素、修改元素、删除元素、检查键或值是否存在,以及遍历元素:

public static void DictionaryOperation()
{
    //创建一个Dictionary来存储学生学号ID和姓名
    Dictionary<int, string> studentDic = new Dictionary<int, string>();

    #region 添加元素

    // Add方法(键必须唯一)
    studentDic.Add(1, "大姚");
    studentDic.Add(2, "小袁");
    studentDic.Add(3, "Edwin");

    // 索引器语法(键不存在时添加,存在时更新)
    studentDic[4] = "Charlie";
    studentDic[5] = "追逐时光者";

    // 安全添加(避免异常)
    bool isAdded = studentDic.TryAdd(6, "小明"); // 返回 false,因键已存在

    #endregion

    #region 访问元素

    // 直接访问(键必须存在,否则会有异常)
    var currentUserName = studentDic[1];
    Console.WriteLine($"当前学生姓名: {currentUserName}");

    // 安全访问(避免异常)
    if (studentDic.TryGetValue(5, outvar getUserName))
    {
        Console.WriteLine($"UserName:{getUserName}");
    }
    else
    {
        Console.WriteLine("当前学生ID不存在");
    }

    #endregion

    #region

    // 修改元素
    studentDic[2] = "大西瓜";

    Console.WriteLine($"修改后的名称:{studentDic[2]}");

    #endregion

    #region 删除元素

    // 删除元素
    bool isRemoved = studentDic.Remove(3);

    Console.WriteLine($"删除结果:{isRemoved}");

    #endregion

    #region 检查键或值是否存在

    // 检查键是否存在
    if (studentDic.ContainsKey(1))
    {
        Console.WriteLine("存在");
    }
    else
    {
        Console.WriteLine("不存在");
    }

    bool isExistcontainsValue = studentDic.ContainsValue("追逐时光者");

    Console.WriteLine($"是否存在:{isExistcontainsValue}");


    #endregion

    #region 遍历元素

    // 遍历元素
    foreach (KeyValuePair<int, string> student in studentDic)
    {
        Console.WriteLine($"ID: {student.Key}, Name: {student.Value}");
    }

    // 使用键的枚举器
    foreach (var key in studentDic.Keys)
    {
        Console.WriteLine($"Key: {key}, Value: {studentDic[key]}");
    }

    // 使用值的枚举器
    foreach (varvaluein studentDic.Values)
    {
        // 注意:这种方式不能直接获取键,只能获取值
        Console.WriteLine($"Value: {value}");
    }

    #endregion
}

参考文章

  • https://learn.microsoft.com/zh-cn/dotnet/api/system.collections.generic.dictionary-2?view=net-9.0
责任编辑:武晓燕 来源: 追逐时光者
相关推荐

2010-03-15 17:56:24

Python字典

2009-05-27 10:12:27

LINQ泛型字典Dictionary

2024-03-12 10:25:14

C#Dictionary编程语言

2020-11-01 17:01:00

Python字典开发

2010-03-03 10:50:22

Python字典应用方

2024-11-15 00:09:21

2021-08-25 11:25:41

MySQLData Dictio数据库

2021-08-26 10:44:31

MySQL MySQL Data 阿里云

2024-12-19 09:00:00

字典视图对象Python

2010-03-15 16:34:50

Python字典

2015-07-28 10:06:03

C#内部实现剖析

2024-04-24 11:27:16

字典推导式Python

2024-11-21 12:00:00

字典缓存Python

2010-05-10 15:22:34

Oracle数据字典

2025-02-11 09:49:12

2024-02-06 09:53:45

Pythonget()函数Dictionary

2024-11-21 09:00:00

Python字典代码

2021-06-12 09:39:50

Python字典数据类型Python基础

2023-12-12 13:55:00

Pythonsubprocess命令

2024-11-18 16:03:36

点赞
收藏

51CTO技术栈公众号