2.3装箱和取消装箱
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Collections;
5
6
7 struct APoint
8 {
9 public int x;
10 public int y;
11 public APoint( int point_x,int point_y)
12
13 {
14 this.x=point_x;
15 this.y =point_y;
16
17 }
18 public void Move(int offset_x, int offset_y)
19 {
20 x += offset_x;
21 y += offset_y;
22
23 }
24 public override string ToString()
25 {
26 return x.ToString() + "," + y.ToString();
27 }//如果没有这行 结果显示 the0的坐标是(APoint)
28
29 static void Main(string[] args)
30 {
31 Console.WriteLine("use ArrayList");
32 ArrayList pnts = new ArrayList(3);
33 for (int i = 0; i < 3; i++)
34 {
35 pnts.Add(new APoint(100, 100));
36 }
37 for (int i = 0; i < 3; i++)
38 {
39 ((APoint)pnts[i]).Move(400,400);
40 }
41 for (int i = 0; i < 3; i++)
42 {
43 Console.WriteLine("the"+i.ToString()+"点坐标是("+pnts[i].ToString()+")");
44
45 }
46 Console.WriteLine("使用数组");
47 APoint[] points = new APoint[3];
48 for (int i = 0; i < 3; i++)
49 {
50 points[i] = new APoint(100,100);
51 }
52 Console.WriteLine("使用foreach循环");
53 foreach (APoint point in points)
54 {
55 point.Move(400,400);
56 }
57 for (int i = 0; i < 3; i++)
58 {
59 Console.WriteLine("第" + i.ToString() + "点坐标是(" + pnts[i].ToString() + ")");
60 }
61 Console.WriteLine("使用for循环");
62 for (int i = 0; i < 3; i++)
63 {
64 points[i].Move(400, 400);
65 }
66 for (int i = 0; i < 3; i++)
67 {
68 Console.WriteLine("第"+i.ToString()+"点坐标是("+points[i].ToString()+")");
69 }
70 Console.ReadLine();
71
结果显示:
use ArrayList
the0点的坐标是(100,100)
the1点的坐标是(100,100)
the2点的坐标是(100,100)
使用数组
使用Foreach
第0点的坐标是(100,100)
第1点的坐标是(100,100)
第2点的坐标是(100,100)
使用For循环
第0点的坐标是(500,500)
第1点的坐标是(500,500)
第2点的坐标是(500,500)