代码改变世界

List<T> myList = new List<T>()有关的方法及属性

2009-04-22 09:59  观海看云  阅读(574)  评论(0编辑  收藏  举报
myList.Capacity       //容量
myList.Count //元素数目
myList.Insert(2,"new item") //插入新元素
myList.Add("new item") //追加元素
mylist.Sort(); //排序
mylist.Reverse(); //反向排序
myList.Remove("new item") //移除指定元素
myList.TrimExcess() //收缩容量。使得Capacity = Count
myList.Contains("new items") //查询是否名含指定内容
myList.Clear() //清除

IList<string> newList = myList.AsReadOnly() //创建myList的一个只读版本
当myList有更改以后newList也跟着变动(指针?)

string[] myCollection = {"one","tow","three","four"}; //定义数组
List<string> mylist = new List<string>(myCollection);
mylist.AddRange(mylist) //自加
mylist.RemoveRange(2,2); //移除
string[] newCollecton = {"one","tow","three","four"};
mylist.InsertRange(3,newCollection) //插入