之前写过一篇关于数组的文章,今天咱们来说一下“个人认为是数组的升级版-集合”。
1,关键字:ArrayList ;在用之前需要先引用一下,例:
using System.Collections;
ArrayList arrayList = new ArrayList
namespace System.Collections
{
//
// 摘要:
// 使用大小会根据需要动态增加的数组来实现 System.Collections.IList 接口。若要浏览此类型的.NET Framework 源代码,请参阅
// Reference Source。
[ComVisible(true)]
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(ArrayListDebugView))]
[DefaultMember("Item")]
public class ArrayList : IList, ICollection, IEnumerable, ICloneable
{
2,之所以说是数组的升级版,是因为在数组里能够做的事情,在集合里照样能够完成,除此之外,集合还能做到数组做不到的一些东西,咱们来看下面的小实例:
class Program
{
static void Main(string[] args)
{
ArrayList arrayList = new ArrayList
{
1,
3,
5,
7,
9,
"walawala",
"哈哈",
'男',
18,
3.14,
85.1325646464646546546
};
//arrayList.Insert(1, 2);
//arrayList.Insert(3, 4);
//arrayList.Insert(5, 6);
//arrayList.Insert(7, 8);
//arrayList.Insert(9, 10);
for (int i = 0; i < arrayList.Count; i++)
{
Console.WriteLine(arrayList[i]);
}
Console.ReadKey();
}
}
在集合里基本上是可以存储任何类型的数据,中间被注掉的部分代码,是集合的插入:Insert 。
3,这篇关于集合的文字就介绍到这里,个人建议,多用,多敲代码,熟练起来会不经意间 get 更多的东西。