数组/集合
数组:在C#中,数组实际上是对象。System.Array是所有数组类型的抽象基类。
- 数组在创建的时候必须指定数组的大小。
int[] numbers = new int[5];
- 初始化数组
int[] numbers = new int[5] {1, 2, 3, 4, 5};
int[] numbers = new int[] {1, 2, 3, 4, 5}; //可省略数组的大小
int[] numbers = {1, 2, 3, 4, 5};//如果提供初始值,还可以省略new运算符
集合
IList 接口和 IDictionary 接口都是从 ICollection 接口派生的
ICollection: Queue/Queue<T>,Stack/Stack<T>,//每个元素都只包含一个值
IList : Array,ArrayList/List<T> //每个元素都只包含一个值
IDictionary: Hashtable/Dictionary<TKey,TValue>,SortedList/SortedList<TKey,TValue> //每个元素都包含一个键和一个值
Queue 类和 Queue<T> 泛型类提供先进先出列表,而 Stack 类和 Stack<T> 泛型类提供后进先出列表。SortedList 类和 SortedList<TKey, TValue> 泛型类提供 Hashtable 类和 Dictionary<TKey, TValue> 泛型类的排序的版本。Hashtable 或 Dictionary<TKey, TValue> 中的元素只能通过元素的键访问;而 SortedList 或 KeyedCollection<TKey, TItem> 中的元素既可以通过元素的键访问,也可以通过元素的索引访问。
示例:
List<T>
List<string> lis = new List<string>();
lis.Add("aaaaaaaa");
lis.Add("bbbbbbbb");
Console.WriteLine(lis[0]);
Dictionary<TKey,TValue>
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("A", "aaaaaaa");
dic.Add("B", "bbbbbbbb");
Console.WriteLine(dic["A"]);
SortedList<TKey,TValue>
SortedList<string, string> sort = new SortedList<string, string>();
sort.Add("A", "aaaaaaaaaaaa");
sort.Add("B", "bbbbbbbbbb");
Console.WriteLine(sort["A"]);
Console.WriteLine(sort.Values[0]);
posted on 2013-01-16 11:02 博客园_net2.0 阅读(140) 评论(0) 收藏 举报
浙公网安备 33010602011771号