C#中的OrderedDictionary
与Dictionary
都是用于存储键值对的集合类型,但它们之间存在一些关键的区别。
OrderedDictionary
- 命名空间:
OrderedDictionary
位于System.Collections.Specialized
命名空间下。 - 存储方式:
OrderedDictionary
没有泛型版本,它使用object
类型来存储键值对。每个元素都是存储在DictionaryEntry
对象中的键/值对。 - 顺序保持:
OrderedDictionary
的一个显著特点是它能够保持元素的插入顺序。这意味着添加键值对到OrderedDictionary
中时,它们将按照添加的顺序被存储和访问。 - 访问方式:可以通过键或索引来访问
OrderedDictionary
中的元素。这使得它在某些需要按顺序访问元素的场景中非常有用。
Dictionary
- 命名空间:
Dictionary
位于System.Collections.Generic
命名空间下。 - 存储方式:
Dictionary
是泛型的,这意味着可以指定键和值的类型。它使用哈希表来实现,因此查找、插入和删除操作的平均时间复杂度是O(1)。 - 顺序保持:
Dictionary
不保证元素的顺序。键值对的存储顺序与键的插入顺序无关。如果需要按键排序的集合,应该考虑使用SortedDictionary
。 - 访问方式:只能通过键来访问
Dictionary
中的元素。如果尝试使用不存在的键进行访问,将引发KeyNotFoundException
异常。
主要区别
- 顺序:
OrderedDictionary
保持插入顺序,而Dictionary
不保证顺序。 - 泛型:
Dictionary
是泛型的,可以提供类型安全,而OrderedDictionary
不是泛型的,使用object
类型存储键值对。 - 访问方式:
OrderedDictionary
可以通过键或索引访问元素,而Dictionary
只能通过键访问元素。 - 性能:由于
Dictionary
是基于哈希表实现的,它在查找、插入和删除操作方面通常比OrderedDictionary
更快。然而,如果需要保持元素的顺序,这种性能差异可能不是最重要的考虑因素。
下面是一个关于C#中OrderedDictionary
使用的具体例子,以及与Dictionary
的对比:
OrderedDictionary 示例
using System;
using System.Collections;
using System.Collections.Specialized;
class Program
{
static void Main()
{
// 创建一个 OrderedDictionary 实例
OrderedDictionary orderedDict = new OrderedDictionary();
// 向 OrderedDictionary 中添加键值对
orderedDict.Add("first", 1);
orderedDict.Add("second", 2);
orderedDict.Add("third", 3);
// 通过键访问元素
Console.WriteLine("通过键访问元素:");
Console.WriteLine("Key 'first' 对应的值: " + orderedDict["first"]);
Console.WriteLine("Key 'second' 对应的值: " + orderedDict["second"]);
// 通过索引访问元素(注意:索引是从0开始的)
Console.WriteLine("\n通过索引访问元素:");
Console.WriteLine("索引 0 对应的值: " + orderedDict[0]);
Console.WriteLine("索引 1 对应的值: " + orderedDict[1]);
// 遍历 OrderedDictionary
Console.WriteLine("\n遍历 OrderedDictionary:");
foreach (DictionaryEntry entry in orderedDict)
{
Console.WriteLine("Key: " + entry.Key + ", Value: " + entry.Value);
}
// 输出 OrderedDictionary 中的键和值集合
Console.WriteLine("\n键集合:");
ICollection keys = orderedDict.Keys;
foreach (object key in keys)
{
Console.WriteLine(key);
}
Console.WriteLine("\n值集合:");
ICollection values = orderedDict.Values;
foreach (object value in values)
{
Console.WriteLine(value);
}
}
}
在这个例子中,我们创建了一个OrderedDictionary
实例,并向其中添加了三个键值对。我们展示了如何通过键和索引来访问元素,以及如何遍历整个OrderedDictionary
。此外,我们还输出了OrderedDictionary
中的键和值集合。
Dictionary 示例(对比)
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// 创建一个 Dictionary 实例
Dictionary<string, int> dict = new Dictionary<string, int>();
// 向 Dictionary 中添加键值对
dict.Add("one", 1);
dict.Add("two", 2);
dict.Add("three", 3);
// 通过键访问元素
Console.WriteLine("通过键访问元素:");
Console.WriteLine("Key 'one' 对应的值: " + dict["one"]);
Console.WriteLine("Key 'two' 对应的值: " + dict["two"]);
// 尝试通过不存在的键访问元素(将引发异常)
// Console.WriteLine("Key 'four' 对应的值: " + dict["four"]); // 这将抛出 KeyNotFoundException
// 遍历 Dictionary
Console.WriteLine("\n遍历 Dictionary:");
foreach (KeyValuePair<string, int> kvp in dict)
{
Console.WriteLine("Key: " + kvp.Key + ", Value: " + kvp.Value);
}
// 获取键和值集合(注意:这些集合是只读的)
Console.WriteLine("\n键集合:");
foreach (string key in dict.Keys)
{
Console.WriteLine(key);
}
Console.WriteLine("\n值集合:");
foreach (int value in dict.Values)
{
Console.WriteLine(value);
}
}
}
在这个Dictionary
的例子中,我们创建了一个泛型Dictionary<string, int>
实例,并向其中添加了三个键值对。我们展示了如何通过键来访问元素,并注意到如果尝试通过不存在的键访问元素,将引发KeyNotFoundException
异常。我们还展示了如何遍历整个Dictionary
,以及如何获取键和值的只读集合。
主要区别回顾
OrderedDictionary
保持插入顺序,而Dictionary
不保证顺序。OrderedDictionary
不是泛型的,使用object
类型存储键值对;而Dictionary
是泛型的,可以提供类型安全。OrderedDictionary
可以通过键或索引访问元素;而Dictionary
只能通过键访问元素。