C#练习4
//错误的程序
using System; class Test { unsafe static void WriteLocations(byte[]arr) { fixed(byte*p_arr=arr) { byte* p_elem = p_arr; for(int i=0;i<arr.Length;i++) byte value=*p_elem; string addr=int.Format((int)p_elem,"x"); Console.WriteLine("arr[{0}]at0x{1}is{2}",i,addr,value); p_elem++; } } } static void Main() { byte[]Array=new byte[]{1,2,3,4,5}; WriteLocations(arr); }
static void Main() { F(); G(); { H(); I(); } } static void Main(String[]args) { if(args.Length==0) goto done; Console.WriteLine(args.Length); done: Console.WriteLine("Done"); } static void Main() { const float pi=3.14; const int r=123; Console.WriteLine(pi*i*i); } static void Main() { int a; int b=2,c=3; a=1; Console.WriteLine(a+b+c); } static int F
using System; class MyClass { public MyClass() { Console.WriteLine("Constructor"); } public MyClass(int value) { MyField = value; Console.WriteLine("Destructor"); } ~MyClass() { Console.WriteLine("Destructor"); } public const int MyConst = 12; public int MyField = 34; public void MyMethod() { Console.WriteLine("MyClass.MyMethod"); } public int Myproperty() { get{ return MyField; } set{ MyField=value; } } public int this[int index]{ get { return 0; } set { Console.WriteLine("this[{0}]{1}", index, value); } } public event EventHandler MyEvent; public static MyClass operator+(MyClass a,MyClass b) { return new MyClass(a.MyField + b.MyField); } internal class MyNestedClass { } } class Constans { public const int A = 1; public const int B = A + 1; } class Test { static void Main() { Console.WriteLine("{0},{1}", Constans.A, Constans.B); } } //Fields class Color { internal ushort redpart; internal ushort bluepart; internal ushort greenpart; public Color(ushort red,ushort blue,ushort green) { redpart = red; bluepart = blue; greenpart = green; } } class Color { public static Color Red = new Color(0XFF, 0, 0); public static Color Blue = new Color(0, 0XFF, 0); public static Color Green = new Color(0, 0, 0XFF); public static Color While = new Color(0, 0, 0); }
using System; class Point { public double x, y; public Point(){ this.x=0; this.y=0; } public Point(double x,double y) { this.x = x; this.y = y; } public static double Distance(Point a,Point b) { double xdiff = a.x - b.x; double ydiff = a.y - b.y; return Math.Sqrt(xdiff * xdiff + ydiff * ydiff); } public override string ToString() { return string.Format("{0},{1}",x,y); } } class Test { static void Main() { Point a = new Point(); Point b = new Point(3, 4); double d = Point.Distance(a, b); Console.WriteLine("Distance from {0} to {1} is {2}", a, b, d); } }
using System; class Point { public double x, y; public Point(double x, double y) { this.x = x; this.y = y; } ~Point() { Console.WriteLine("Destructed {0}", this); } public override string ToString() { return string.Format("{0},{1}",x,y); } }
using System; class Test { static void Main() { Type type = typedef(Class1); object[] arr = type.GetCustomAttributes(typeof(HelpAttribute)); if (arr.Length == 0) Console.WriteLine("Class1 has no Help attribute."); else { HelpAttribute ha = (HelpAttribute)arr[0]; Console.WriteLine("Ur1={0},Topic={1}", ha.Ur1, ha.Topic); } } }
using System; class Test { static void Main() { Type type = typedef(Class1); object[] arr = type.GetCustomAttributes(typeof(HelpAttribute)); if (arr.Length == 0) Console.WriteLine("Class1 has no Help attribute."); else { HelpAttribute ha = (HelpAttribute)arr[0]; Console.WriteLine("Ur1={0},Topic={1}", ha.Ur1, ha.Topic); } } }