Dictionary 概述
-
Dictionary<TKey, TValue>
存储的是键值对(Key - Value),通过键(Key)来存储或修改值(Value) -
Dictionary<TKey, TValue>
存储的键值对是无序的 -
Dictionary<TKey, TValue>
存储的键是不可重复的 -
Dictionary<TKey, TValue>
支持泛型,可以指定存储的键值对的类型 -
Dictionary<TKey, TValue>
不是线程安全的,在多线程环境中需要谨慎使用
一、Dictionary 存储对象的特性
- 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
- 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 转数组
- 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
- 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
- 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
标签:容器,Dictionary,C#,30,Alice,Add,new,Staff
From: https://blog.csdn.net/weixin_52173250/article/details/143659642