在.net framework 2.0之前,数组表示可以用Array 和集合类 ArrayList 表示,2.0后.net 引进了泛型的概念List<>,2.0引进泛型其本意是减少类型的装箱拆箱所带来的性能消耗。
ArrayList速度是最慢的它只能add object type。array的效率要比List的要高一些,当数组长度不是很大时,两者没什么区别,建议用List<>,毕竟是可变长度,可以Add;特殊应用还是建议用array,不提倡用ArrayList.[http://www.cnblogs.com/criedshy/archive/2010/06/01/1749272.html]
一,C# array数组的用法范例:
type[] typename=new type[size];
(1):type数据类型不能缺;且要统一,而不能是如 int[] a=new Array[];
(2):数组的大小size不能缺,否则c#认为是出错的,因为数组是一段固定长度的内存;
(3):右边是一个中括号[],而不是()
二,C# ArrayList数组的用法范例:
ArrayList al = new ArrayList();
for (int i = 0; i < 3; i++)
{
al.Add(i);
}
三,ArrayList和Array相互之间的转化
(1)以下是把ArrayList数组中的值拷贝到Array中去的实例用法
//int[] IResult=new int[al.Count];
//al.CopyTo(IResult);
//或是用下面的方法
int[] IResult = (int[])al.ToArray(typeof(Int32));//ToArray(Int32);这样错误,一定要强制类型转换
//Person[] personArrayFromList = (Person[])personList.ToArray(typeof(Person));
foreach(int item in IResult)
{
Response.Write(item.ToString());
}
(2)以下是把Array数组中的值拷贝到ArrayList中去的实例用法
int[] a ={ 222, 333, 555 };
ArrayList arrList = new ArrayList();
foreach (object obj in a)//或foreach (int obj in a)
{
arrList.Add(obj);
}
var aa= (from b in bbb select b).Distinct();
var lstResult = new List<Person>();
lstResult.Add(new Person()
{
Id = "",
Name = "",
Age = "",
Sex = ""
});
3.Lambda表达式与List
list.Find(v => v.id==1);//选择单条记录
list.FindAll(v => v.id==1);// 选择多条记录
list.Exists(v => v.id==1); //检查是否存在,返回Bool
list.Count(v => v.id==1);//计数
list.ForEach(v => v.id=1);//循环赋值