在 C# 中,Dictionary
是一个非常常用的数据结构,用于存储键值对。Dictionary
类实现了 IDictionary
接口,并且提供了许多有用的方法和属性来操作键值对集合。下面是一些关于如何使用 Dictionary
的详细说明:
Csharp
深色版本
1Dictionary myDictionary = new Dictionary();
或者使用字面量语法:
Csharp
深色版本
1Dictionary myDictionary = new Dictionary
2{
3 {"one", 1},
4 {"two", 2},
5 {"three", 3}
6};
Csharp
深色版本
1myDictionary.Add("four", 4);
或者使用索引器:
Csharp
深色版本
1myDictionary["five"] = 5;
注意:如果键已经存在,则使用索引器会覆盖原有的值。
Csharp
深色版本
1int value;
2if (myDictionary.TryGetValue("one", out value))
3{
4 Console.WriteLine(value); // 输出 1
5}
6else
7{
8 Console.WriteLine("Key not found.");
9}
或者直接使用索引器:
Csharp
深色版本
1Console.WriteLine(myDictionary["one"]); // 输出 1
如果键不存在,使用索引器会抛出 KeyNotFoundException
。
Csharp
深色版本
1myDictionary["one"] = 10; // 更新值
Csharp
深色版本
1myDictionary.Remove("one"); // 删除键为 "one" 的元素
Csharp
深色版本
1foreach (KeyValuePair kvp in myDictionary)
2{
3 Console.WriteLine($"Key = {kvp.Key}, Value = {kvp.Value}");
4}
获取字典中键值对的数量:
Csharp
深色版本
1int count = myDictionary.Count;
获取字典中所有的键或值:
Csharp
深色版本
1foreach (string key in myDictionary.Keys)
2{
3 Console.WriteLine(key);
4}
5
6foreach (int value in myDictionary.Values)
7{
8 Console.WriteLine(value);
9}
清空字典中的所有元素:
Csharp
深色版本
1myDictionary.Clear();
如果您需要在 Dictionary
中存储具有特定特性的类型,可以使用泛型约束:
Csharp
深色版本
1public class MyClass where TKey : IComparable
2{
3 private Dictionary _dictionary;
4
5 public MyClass()
6 {
7 _dictionary = new Dictionary();
8 }
9
10 public void Add(TKey key, TValue value)
11 {
12 _dictionary.Add(key, value);
13 }
14
15 public TValue GetValue(TKey key)
16 {
17 TValue value;
18 _dictionary.TryGetValue(key, out value);
19 return value;
20 }
21}
如果需要对字典进行排序,可以使用 LINQ:
Csharp
深色版本
1var sortedDictionary = from entry in myDictionary
2 orderby entry.Key
3 select entry;
4
5foreach (var item in sortedDictionary)
6{
7 Console.WriteLine($"Key = {item.Key}, Value = {item.Value}");
8}
如果尝试向字典中添加重复的键,Add
方法会抛出异常。您可以使用 TryAdd
方法来避免这种情况:
Csharp
深色版本
1bool added = myDictionary.TryAdd("one", 1);
当尝试从字典中获取不存在的键时,使用索引器会抛出异常。为了避免这种情况,可以使用 TryGetValue
方法:
Csharp
深色版本
1int value;
2if (myDictionary.TryGetValue("one", out value))
3{
4 Console.WriteLine(value);
5}
6else
7{
8 Console.WriteLine("Key not found.");
9}
如果您需要一个线程安全的字典,可以使用 ConcurrentDictionary
:
Csharp
深色版本
1ConcurrentDictionary concurrentDictionary = new ConcurrentDictionary();
2
3concurrentDictionary.TryAdd("one", 1);
4concurrentDictionary.TryAdd("two", 2);
5
6int value;
7concurrentDictionary.TryGetValue("one", out value);
下面是一个完整的示例代码,展示了如何使用 Dictionary
:
Csharp
深色版本
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6 static void Main()
7 {
8 Dictionary myDictionary = new Dictionary
9 {
10 {"one", 1},
11 {"two", 2},
12 {"three", 3}
13 };
14
15 // 添加新元素
16 myDictionary.Add("four", 4);
17
18 // 更新值
19 myDictionary["one"] = 10;
20
21 // 获取值
22 int value;
23 if (myDictionary.TryGetValue("one", out value))
24 {
25 Console.WriteLine(value); // 输出 10
26 }
27 else
28 {
29 Console.WriteLine("Key not found.");
30 }
31
32 // 删除元素
33 myDictionary.Remove("two");
34
35 // 遍历字典
36 foreach (KeyValuePair kvp in myDictionary)
37 {
38 Console.WriteLine($"Key = {kvp.Key}, Value = {kvp.Value}");
39 }
40
41 // 清空字典
42 myDictionary.Clear();
43 }
44}