arraylist x = A;string y = x[0].tostring().trim();A是一个arraylist数组,里面只有一个值为“167”,通过上述的程序,不知道为何会出现错误提示,出现结果为y的值为“System.Collections.arraylist”,而不是“167”,这是为何?
LZ犯了一个致命的错误。ArrayList集合是位于System.Generic.Collections命名空间下的集合类。 它可以存储任意类型的数据。 所有存入ArrayList的数据都会变为Object类型,那么这个过程对于值类型来说就是一个装箱(Boxing)。 当我想从ArrayList中读取出数据的时候,我需要将数据由Object类型转换为原本的类型,对于值类型来说,这又是一个拆箱(UnBoxing)。Boxing和UnBoxing非常影响程序的性能。实例代码:ArrayList list = new ArrayList(); //实例化几个Student类对象 Student rose = new Student("rose",23,"reading"); Student jack = new Student("jack",25,"singing"); Student doon = new Student("doon",27,"dancing");list.Add(rose); list.Add(jack); list.Add(doon);//读取数据,注意:这里需要进行类型转换 Student a = list[0] as Student;
Graphics graphics = e.Graphics; graphics.SmoothingMode = SmoothingMode.AntiAlias; SolidBrush brush = new SolidBrush(Color.Blue); Point point1 = new Point(100, 100); Rectangle rectangle = new Rectangle(point1, new Size(15, 15)); graphics.DrawEllipse(Pens.Blue, rectangle); graphics.FillEllipse(brush, rectangle);
我测试怎么没有错误
A.Add("xml");
ArrayList bb = A;
Console.WriteLine(bb[0].ToString());
Console.ReadLine();输出结果是xml
它可以存储任意类型的数据。
所有存入ArrayList的数据都会变为Object类型,那么这个过程对于值类型来说就是一个装箱(Boxing)。
当我想从ArrayList中读取出数据的时候,我需要将数据由Object类型转换为原本的类型,对于值类型来说,这又是一个拆箱(UnBoxing)。Boxing和UnBoxing非常影响程序的性能。实例代码:ArrayList list = new ArrayList();
//实例化几个Student类对象
Student rose = new Student("rose",23,"reading");
Student jack = new Student("jack",25,"singing");
Student doon = new Student("doon",27,"dancing");list.Add(rose);
list.Add(jack);
list.Add(doon);//读取数据,注意:这里需要进行类型转换
Student a = list[0] as Student;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
SolidBrush brush = new SolidBrush(Color.Blue);
Point point1 = new Point(100, 100);
Rectangle rectangle = new Rectangle(point1, new Size(15, 15));
graphics.DrawEllipse(Pens.Blue, rectangle);
graphics.FillEllipse(brush, rectangle);