1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 /* 7 * C# 中 结构 struct 与类 class的部分区别 8 * 9 * 结构中不能声明无参的构造函数 系统会自动给出无参的构造函数 10 * 结构中的可访问性只能是 public 11 * 结构允许不使用new 运算符 12 * 结构是值类型 结构对象存放在栈当中,类是引用类型 类对象存放在堆当中 13 * 结构效率更高,适用于简单的数据结构 14 * 结构不能继承类 也不能作为一个类的基 15 */ 16 namespace 结构 17 { 18 struct Point 19 { 20 public int x; 21 public int y; 22 public Point(int i,int j) 23 { 24 x = i; 25 y = j; 26 } 27 } 28 class CPoint 29 { 30 private int x; //非public 31 private int y; 32 public CPoint(int i, int j) 33 { 34 x = i; 35 y = j; 36 } 37 public int X 38 { 39 get { return x;} 40 } 41 public int Y 42 { 43 get { return y; } 44 } 45 public CPoint() //不带参数的构造函数 46 { 47 x=-1; 48 y=-1; 49 } 50 } 51 class Program 52 { 53 static void Main(string[] args) 54 { 55 Point[] p = { new Point(1, 1), new Point(1, 2), new Point(1, 3), new Point(1, 4), new Point(1, 5), new Point(1, 6), 56 new Point(1,7), new Point(1,8), new Point(1,9), new Point(1,10)}; 57 Point[] p1 = new Point[10]; //struct 系统自动给出无参的构造函数 58 CPoint[] p2 = { new CPoint(), new CPoint(), new CPoint(), new CPoint(), new CPoint(), 59 new CPoint(),new CPoint(),new CPoint(),new CPoint(),new CPoint(),}; 60 CPoint[] p3 = { new CPoint(3, 1), new CPoint(3, 2), new CPoint(3, 3), new CPoint(3, 4), new CPoint(3, 5), 61 new CPoint(3,6),new CPoint(3,7),new CPoint(3,8),new CPoint(3,9),new CPoint(3,10),}; 62 Console.WriteLine("p 中的元素值为"); 63 foreach (Point i in p) 64 Console.WriteLine(i.x+" "+i.y); 65 Console.WriteLine("p1 中的元素值为"); 66 foreach (Point j in p1) 67 Console.WriteLine(j.x+" "+j.y); 68 Console.WriteLine("p2 中的元素值为"); 69 foreach (CPoint k in p2) 70 Console.WriteLine(k.X+" "+k.Y); 71 Console.WriteLine("p3 中的元素值为"); 72 foreach (CPoint l in p3) 73 Console.WriteLine(l.X+" "+l.Y); 74 } 75 } 76 }
原文:http://www.cnblogs.com/paopaoer/p/4492533.html