欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > 4-3-2.C# 数据容器 - Dictionary 扩展(Dictionary 存储对象的特性、Dictionary 与数组的转换)

4-3-2.C# 数据容器 - Dictionary 扩展(Dictionary 存储对象的特性、Dictionary 与数组的转换)

2025/2/25 3:18:01 来源:https://blog.csdn.net/weixin_52173250/article/details/143659642  浏览:    关键词:4-3-2.C# 数据容器 - Dictionary 扩展(Dictionary 存储对象的特性、Dictionary 与数组的转换)

Dictionary 概述

  1. Dictionary<TKey, TValue> 存储的是键值对(Key - Value),通过键(Key)来存储或修改值(Value)

  2. Dictionary<TKey, TValue> 存储的键值对是无序的

  3. Dictionary<TKey, TValue> 存储的键是不可重复的

  4. Dictionary<TKey, TValue> 支持泛型,可以指定存储的键值对的类型

  5. Dictionary<TKey, TValue> 不是线程安全的,在多线程环境中需要谨慎使用


一、Dictionary 存储对象的特性

  1. Dictionary 存储对象或对对象进行检测时(对象作为键或值时),对于没有重写 Equals 和 GetHashCode 方法的对象可能不太方便
internal class Person
{public String name;public int age;public Person() { }public Person(string name, int age){this.name = name;this.age = age;}
}
Dictionary<Person, string> messageDict = new Dictionary<Person, string>();messageDict.Add(new Person("Alice", 30), "Hello World 1");
messageDict.Add(new Person("Bob", 25), "Hello World 2");Console.WriteLine(messageDict.ContainsKey(new Person("Alice", 30)));
Console.WriteLine(messageDict.ContainsKey(new Person("Alice", 31)));
# 输出结果False
False
Dictionary<string, Person> personDict = new Dictionary<string, Person>();personDict.Add("Alice", new Person("Alice", 30));
personDict.Add("Bob", new Person("Bob", 25));Console.WriteLine(personDict.ContainsValue(new Person("Alice", 30)));
Console.WriteLine(personDict.ContainsValue(new Person("Alice", 31)));
# 输出结果False
False
  1. Dictionary 存储对象或对对象进行检测时(对象作为键或值时),对于重写 Equals 和 GetHashCode 方法的对象比较方便
internal class Staff
{public String name;public int age;public Staff() { }public Staff(string name, int age){this.name = name;this.age = age;}// 重写 Equals 方法public override bool Equals(object obj){// 检查是否为同一个对象的引用if (obj == this) return true;// 检查对象是否为空if (obj == null) return false;// 检查类型是否相同if (obj.GetType() != this.GetType()) return false;// 将 obj 转换为 Staff 类型并进行属性比较Staff staff = obj as Staff;bool agesAreEqual = age == staff.age;bool namesAreEqual = name == null ? null == staff.name : name.Equals(staff.name);return agesAreEqual && namesAreEqual;}public override int GetHashCode(){// 使用属性生成哈希码int hash = 17;hash = hash * 23 + name?.GetHashCode() ?? 0;hash = hash * 23 + age.GetHashCode();return hash;}
}
Dictionary<Staff, string> messageDict = new Dictionary<Staff, string>();messageDict.Add(new Staff("Alice", 30), "Hello World 1");
messageDict.Add(new Staff("Bob", 25), "Hello World 2");Console.WriteLine(messageDict.ContainsKey(new Staff("Alice", 30)));
Console.WriteLine(messageDict.ContainsKey(new Staff("Alice", 31)));
# 输出结果True
False
Dictionary<string, Staff> staffDict = new Dictionary<string, Staff>();staffDict.Add("Alice", new Staff("Alice", 30));
staffDict.Add("Bob", new Staff("Bob", 25));Console.WriteLine(staffDict.ContainsValue(new Staff("Alice", 30)));
Console.WriteLine(staffDict.ContainsValue(new Staff("Alice", 31)));
# 输出结果True
False

二、Dictionary 与数组的转换

1、Dictionary 转数组
  1. Dictionary 转键值对数组
Dictionary<string, int> dict = new Dictionary<string, int>();dict.Add("Alice", 30);
dict.Add("Bob", 25);KeyValuePair<string, int>[] array = dict.ToArray();foreach (KeyValuePair<string, int> kvp in array)
{Console.WriteLine($"{kvp.Key} - {kvp.Value}");
}
# 输出结果Alice - 30
Bob - 25
  1. Dictionary 转键数组
Dictionary<string, int> dict = new Dictionary<string, int>();dict.Add("Alice", 30);
dict.Add("Bob", 25);KeyValuePair<string, int>[] array = dict.ToArray();foreach (KeyValuePair<string, int> kvp in array)
{Console.WriteLine($"{kvp.Key} - {kvp.Value}");
}
# 输出结果Alice
Bob
  1. Dictionary 转值数组
Dictionary<string, int> dict = new Dictionary<string, int>();dict.Add("Alice", 30);
dict.Add("Bob", 25);string[] array = dict.Keys.ToArray();foreach (string item in array)
{Console.WriteLine(item);
}
# 输出结果30
25
2、数组转 Dictionary
var array = new (string, int)[]
{("Alice", 30),("Bob", 25)
};Dictionary<string, int> dict = array.ToDictionary(kvp => kvp.Item1, kvp => kvp.Item2);foreach (KeyValuePair<string, int> kvp in dict)
{Console.WriteLine($"{kvp.Key} - {kvp.Value}");
}
# 输出结果Alice - 30
Bob - 25

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词