如何使用C#创建字典?
字典是C#中键和值的集合。Dictionary<TKey,TValue>包含在System.Collection.Generics命名空间中。
要创建字典,您首先需要对其进行设置并添加键和值。在这里,我们为字典添加了5个带有值的键。我们将其键和值类型设置为int。
IDictionary<int, int> d = new Dictionary<int, int>(); d.Add(1,44); d.Add(2,34); d.Add(3,66); d.Add(4,47); d.Add(5,76);
以下是完整的代码-
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
IDictionary<int, int> d = new Dictionary<int, int>();
d.Add(1,44);
d.Add(2,34);
d.Add(3,66);
d.Add(4,47);
d.Add(5,76);
Console.WriteLine(d.Count);
}
}热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短