首页 > 编程笔记 > C#笔记 阅读:6

C# Dictionary集合的用法(附带实例)

C# 中的 Dictionary 是一个字典格式的集合,每个字典元素都由 <TKey, TValue> 组成,可以想成 <键,值> 配对元素,其中键是唯一的不可以是 null,值则是可以重复的或是 null。

C# Dictionary对象的创建

如果只是创建 Dictionary 对象,则可以使用下列语法:
Dictionary<TKey, TValue> 对象名称 = new Dictionary<TKey, TValue>();

如果要创建 Dictionary 对象,同时还要设定此对象的容量,则可以增加 Int32 参数,详情可以参考下列语法:
Dictionary<TKey, TValue> 对象名称 = new Dictionary<TKey, TValue>(Int32);

如果要创建对象,同时设置初始值,则可以使用下列语法:
Dictionary<TKey, TValue> 对象名称 = new Dictionary<TKey, TValue> { 系列初值 }

【实例 1】创建键是整数和字符串,值则是字符串的 Dictionary 对象,然后输出,声明时第 1 行采用标准声明,第 10 行采用 var 简化声明。输出时第 8 行使用中规中矩的方式来声明 n,第 18 行则使用简化 var 方式来声明 n,当输出时 n.Key 表示输出元素的键,n.Value 表示输出元素的值。
Dictionary<int, string> number = new Dictionary<int, string>()
{
    {1, "one"},
    {2, "two"},
    {3, "three"}
};
foreach (KeyValuePair<int, string> n in number)
    Console.WriteLine($"Key:{n.Key}, Value:{n.Value}");

var season = new Dictionary<string, string>()
{
    {"春季", "Spring"},
    {"夏季", "Summer"},
    {"秋季", "Autumn"},
    {"冬季", "Winter"}
};
foreach (var n in season)
    Console.WriteLine($"Key:{n.Key}, Value:{n.Value}");
执行结果为:

Key:1, Value:one
Key:2, Value:two
Key:3, Value:three
Key:春季, Value:Spring
Key:夏季, Value:Summer
Key:秋季, Value:Autumn
Key:冬季, Value:Winter

C# Dictionary的属性

Dictionary 的常用属性如下:
【实例 2】获得 Dictionary 对象的 Keys 属性。
Dictionary<int, string> number = new Dictionary<int, string>()
{
    {1, "one"},
    {2, "two"},
    {3, "three"}
};
Dictionary<int, string>.KeyCollection numberKeys = number.Keys;
foreach (int n in numberKeys)
    Console.WriteLine($"Key:{n}");
var season = new Dictionary<string, string>()
{
    {"春季", "Spring"},
    {"夏季", "Summer"},
    {"秋季", "Autumn"},
    {"冬季", "Winter"}
};
Dictionary<string, string>.KeyCollection seasonKeys = season.Keys;
foreach (string n in seasonKeys)
    Console.WriteLine($"Key:{n}");
执行结果为:

Key:1
Key:2
Key:3
Key:春季
Key:夏季
Key:秋季
Key:冬季


上述读者需要留意的是第 7 行声明键值的变量 numberKeys,如下:
Dictionary<int, string>.KeyCollection numberKeys = number.Keys;

第 17 行声明键值的变量 seasonKeys,如下:
Dictionary<string, string>.KeyCollection seasonKeys = season.Keys;

其实上述第 7 行和第 17 行的声明可以省略,在第 8 行和第 18 行的 foreach 内,直接可以遍历 number.Keys 和 season.Keys 即可。


【实例 3】简化方式设计实例 2。
Dictionary<int, string> number = new Dictionary<int, string>()
{
    {1, "one"},
    {2, "two"},
    {3, "three"}
};
//Dictionary<int, string>.KeyCollection numberKeys = number.Keys;
foreach (int n in numberKeys)
    Console.WriteLine($"Key:{n}");
var season = new Dictionary<string, string>()
{
    {"春季", "Spring"},
    {"夏季", "Summer"},
    {"秋季", "Autumn"},
    {"冬季", "Winter"}
};
//Dictionary<string, string>.KeyCollection seasonKeys = season.Keys;
foreach (string n in seasonKeys)
    Console.WriteLine($"Key:{n}");
执行结果与实例 2 相同。

【实例 4】获得 Dictionary 对象的 Values 属性。
Dictionary<int, string> number = new Dictionary<int, string>()
{
    {1, "one"},
    {2, "two"},
    {3, "three"}
};
Dictionary<int, string>.ValueCollection numberValues = number.Values;
foreach (string n in numberValues)
    Console.WriteLine($"Value:{n}");
var season = new Dictionary<string, string>()
{
    {"春季", "Spring"},
    {"夏季", "Summer"},
    {"秋季", "Autumn"},
    {"冬季", "Winter"}
};
Dictionary<string, string>.ValueCollection seasonValues = season.Values;
foreach (string n in seasonValues)
    Console.WriteLine($"Value:{n}");
执行结果为:

Value:one
Value:two
Value:three
Value:Spring
Value:Summer
Value:Autumn
Value:Winter

上述读者需要留意的是第 7 行声明键值的变量 numberValues,如下:
Dictionary<int, string>.ValueCollection numberValues = number.Values;

第 17 行声明键值的变量 seasonValues,如下:
Dictionary<string, string>.KeyCollection seasonValues = season.Values;
其实上述第 7 和 17 行声明是可以省略,在第 8 和 18 行的 foreach 内,直接可以遍历 number.Values 和 season.Values 即可。

【实例 5】简化方式设计实例 4。
Dictionary<int, string> number = new Dictionary<int, string>()
{
    {1, "one"},
    {2, "two"},
    {3, "three"}
};
//Dictionary<int, string>.ValueCollection numberValues = number.Values;
foreach (string n in number.Values)
    Console.WriteLine($"Value:{n}");

var season = new Dictionary<string, string>()
{
    {"春季", "Spring"},
    {"夏季", "Summer"},
    {"秋季", "Autumn"},
    {"冬季", "Winter"}
};
//Dictionary<string, string>.ValueCollection seasonValues = season.Values;
foreach (string n in season.Values)
    Console.WriteLine($"Value:{n}");
执行结果与实例 4 相同。

C# Dictionary的方法

Dictionary 的常用方法如下:
除了上述方法,还可以使用下列方式取得并设定字典配对值:
字典变量[Key] = xx;

【实例 6】创建 Dictionary 对象。
Dictionary<int, string> number = new Dictionary<int, string>();
number.Add(1, "One");  // 使用 Add() 方法
number.Add(2, "Two");
number.Add(3, "Three");
Console.WriteLine($"number[1] : {number[1]}");
Console.WriteLine($"number[2] : {number[2]}");
Console.WriteLine($"number[3] : {number[3]}");

Dictionary<string, string> season = new Dictionary<string, string>();
season.Add("春季", "Spring");  // 使用 Add() 方法
season.Add("夏季", "Summer");
Console.WriteLine($"season[春季] : {season["春季"]}");
Console.WriteLine($"season[夏季] : {season["夏季"]}");
执行结果为:

number[1] : One
number[2] : Two
number[3] : Three
season[春季] : Spring
season[夏季] : Summer


【实例 7】使用直接设定方式创建字典内容。
Dictionary<int, string> number = new Dictionary<int, string>();
number.Add(1, "One");        // 使用 Add() 方法
number.Add(2, "Two");
number[3] = "three";        // 直接设置
Console.WriteLine($"number[1] : {number[1]}");
Console.WriteLine($"number[2] : {number[2]}");
Console.WriteLine($"number[3] : {number[3]}");

Dictionary<string, string> season = new Dictionary<string, string>();
season.Add("春季", "Spring");  // 使用 Add() 方法
season["夏季"] = "Summer";    // 直接设置
Console.WriteLine($"season[春季] : {season["春季"]}");
Console.WriteLine($"season[夏季] : {season["夏季"]}");
执行结果为:

number[1] : One
number[2] : Two
number[3] : three
season[春季] : Spring
season[夏季] : Summer

读者需留意第 4 行和第 11 行字典内容的设定。

相关文章