C# 入门之字典

今天我们将来一起看一下 C# 中的另外一种数据类型:字典。

字典,是一组 key 和 value 的映射,且 key 是唯一的。

字典包含在 System.Collections.Generic namespace。

下面我们来看一下,如何创建一个字典:

using System;
using System.Collections.Generic;

namespace ConsoleApp14
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个空字典
            Dictionary<string, string> students = new Dictionary<string, string>();  
            students.Add("00001", "Tom");      // 添加数据到字典(key 和 value)
            students.Add("00002", "Jerry");
            Console.WriteLine($"00001 is {students["00001"]}");  // 通过 key 查询 value
        }
    }
}

运行结果:
https://ithelp.ithome.com.tw/upload/images/20210913/20099494gWwNdxNiDO.png

我们再来看看字典的其他操作:

using System;
using System.Collections.Generic;

namespace ConsoleApp14
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> students = new Dictionary<string, string>();
            students.Add("00001", "Tom");
            students.Add("00002", "Jerry");
            Console.WriteLine($"00001 is {students["00001"]}");
            foreach (var (key,value) in students)  // 变量字典中的 key 和 value
            {
                Console.WriteLine($"{key} is {value}");
            }
            Console.WriteLine("-----------------------");
            foreach (var student in students)    // 遍历字典中的 key 和 value 的另一种方法
            {
                Console.WriteLine($"{student.Key} is {student.Value}");
            }
            Console.WriteLine("-----------------------");
            foreach (var val in students.Values)      // 遍历 value
            {
                Console.WriteLine($"{val}");
            }
            Console.WriteLine("-----------------------");
            foreach (var key in students.Keys)    // 遍历 key
            {
                Console.WriteLine($"{key}");
            }
            Console.WriteLine("-----------------------");
            Console.WriteLine($"{students.ContainsKey("00002")}"); // 查询字典中是否存在某个 key
            Console.WriteLine($"{students.ContainsValue("Tom")}"); // 查询字典中是否存在某个 value
            Console.WriteLine($"{students.Count}");        // 计算字典中的 key value 对的个数
            Console.WriteLine("-----------------------");
            students.Remove("00001");        // 根据 key 删除 key value 对
            foreach (var (key, value) in students)
            {
                Console.WriteLine($"{key} is {value}");
            }
            students.Clear();          //  清空字典
            Console.WriteLine($"{students.Count}");
        }
    }
}

运行结果:
https://ithelp.ithome.com.tw/upload/images/20210913/20099494H8jWC7ce7P.png


<<:  Day-1 杰哥的考研纪录

>>:  Day 1【序】我现在是天真无邪的学生

【D2】要下厨前需要准备锅具

简介厨房:Shioaji Shioaji是永丰证开发出来的Python API,用来给客户自行开发自...

D07 - 听话,给我资料!

既然已经透过 Serial API 取得 Port 存取权限了,再来我们就要来接收并解析资料了。 建...

Day13-290. Word Pattern

今日题目:290. Word Pattern(Easy) Given a pattern and a...

sed - 5 Replace command

前篇回顾 sed - 简介 读取编辑文字档的好工具 sed - 2 Pattern sed - 3 ...

【Day29 #1】企业数位治理议题2:企业经营流程之数位化整合

#odoo #开源系统 #数位赋能 #E化自主 前言 昨天我们利用个案协助大家认识对於一个正在成长中...