字典与数组在 C# 中有什么区别?
字典
字典是 C# 中的键值集合。字典包含在 System.Collection.Generics 命名空间中。
声明字典的代码:
IDictionary<int, int> d = new Dictionary<int, int>();
添加元素的代码:
IDictionary<int, int> d = new Dictionary<int, int>(); d.Add(1,97); d.Add(2,89); d.Add(3,77); d.Add(4,88);
数组
数组存储相同类型元素的固定大小的顺序集合。它由连续的内存位置组成。最低地址对应于第一个元素,最高地址对应于最后一个元素。
定义数组的代码:
int[] arr = new int[5];
初始化并向数组中设置元素
int[] arr = new int[10] {3, 5, 35, 87, 56, 99, 44, 36, 78};
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP