C#中添加对象到ArrayList的代码

把开发过程中比较好的一些代码段做个备份,下面代码是关于C#中添加对象到ArrayList的代码。

ArrayList alcollect = new ArrayList();
string str = "learn csharp";
alcollect.Add(str);
alcollect.Add("hello world");
alcollect.Add(500);
alcollect.Add(new object());





AddRange方法支持添加一个范围内的对象。


ArrayList alcollect = new ArrayList();
string[] someArray = new string[] { "ek", "do", "theen" };
alcollect.AddRange(someArray);





Add和AddRange将对象添加到ArrayList的末尾。Insert和InsertRange方法添加对象到指定的索引位置。


ArrayList alcollect = new ArrayList();
alcollect.Insert(3, "adding this at 3rd posn");
string[] someStrings = new string[] { "hello", "world" };
alcollect.InsertRange(4, someStrings);





我们可以通过索引号找到指定的对象


ArrayList alcollect = new ArrayList();
alcollect[3] = "iam at three";





猜你喜欢

转载自www.cnblogs.com/SHUN019/p/10225544.html