1.ABeginnersHelloWorld初学者代码publicclassHelloWorld{publicstaticvoidMain(){System.Console.WriteLine(HELLOWORLD);}}2.Slightlyimprovedversion略有提高代码usingSystem;(就这?会用命名空间?)publicclassHelloWorld{publicstaticvoidMain(){Console.WriteLine(HELLOWORLD);}}3.CommandLineArguments命令行参数代码usingSystem;publicclassHelloWorld{publicstaticvoidMain(string[]args)//会传参数了{Console.WriteLine(args[0]);}}4.FromConstructor构造函数代码usingSystem;publicclassHelloWorld{publicHelloWorld(){Console.WriteLine(HELLOWORLD);}publicstaticvoidMain(){HelloWorldhw=newHelloWorld();//会用类了?构造?}}5.MoreOO代码usingSystem;publicclassHelloWorld{publicvoidhelloWorld(){Console.WriteLine(HELLOWORLD);}publicstaticvoidMain(){HelloWorldhw=newHelloWorld();hw.HelloWorld();//更进一步的面向对象?会用方法了?}}6.Fromanotherclass调用另一个类代码usingSystem;publicclassHelloWorld{publicstaticvoidMain(){HelloWorldHelperClasshwh=newHelloWorldHelperClass();//类里调用其它类?hwh.writeHelloWorld();}}publicclassHelloWorldHelperClass{publicvoidwriteHelloWorld(){Console.WriteLine(HelloWorld);}}7.Inheritance继承代码abstractclassHelloWorldBase//抽象类{publicabstractvoidwriteHelloWorld();}classHelloWorld:HelloWorldBase//继承----不得不严肃起来了,能抽象的已经可以做系统架构设计了!{publicoverridevoidwriteHelloWorld(){Console.WriteLine(HelloWorld);Console.ReadLine();}}classHelloWorldImp{staticvoidMain(){HelloWorldBasehwb=newHelloWorld();hwb.writeHelloWorld();}}8.StaticConstructor静态构造函数代码usingSystem;publicclassHelloWorld{privatestaticstringstrHelloWorld;staticHelloWorld()//静态构造{strHelloWorld=HelloWorld;}voidwriteHelloWorld(){Console.WriteLine(strHelloWorld);}publicstaticvoidMain(){HelloWorldhw=newHelloWorld();//需要吗?hw.writeHelloWorld();//平常我会觉得很可笑----居然写得这么啰嗦}}9.ExceptionHandling异常处理代码usingSystem;publicclassHelloWorld{publicstaticvoidMain(string[]args){try{Console.WriteLine(args[0]);}catch(IndexOutOfRangeExceptione)//会用异常处理了,但如何更好回收资源呢?异常接下来应该是资源回收啊?我以前也犯这种毛病,GC应该怎么更好使用,我到现在还不是很纯熟{Console.WriteLine(e.ToString());}}10.CreatingaDLLandusingitinanapplication做组件吗?代码usingSystem;namespaceHelloLibrary{publicclassHelloMessage{publicstringMessage{get{returnHello,World!!!;}}}}//------代码usingSystem;usingHelloLibrary;namespaceHelloApplication{classHelloApp{publicstaticvoidMain(string[]args){HelloMessagem=newHelloMessage();}}}11.UsingProperty实用属性代码usingSystem;publicclassHelloWorld{publicstringstrHelloWorld{get//会用属性了{returnHelloWorld;}}publicstaticvoidMain(){HelloWorldhw=newHelloWorld();Console.WriteLine(hw.strHelloWorld);}}12.UsingDelegates//委托!代码usingSystem;classHelloWorld{delegatevoidSimpleDelegate();//定义委托staticvoidwriteHelloWorld(){Console.WriteLine(HelloWorld);}staticvoidMain(){SimpleDelegated=newSimpleDelegate(writeHelloWorld);//委托?!?!d();//语法的确这么写,但含义无法理解;因为实在体会不出好处来}}13.UsingAttributes//我不会!补习去!代码#defineDEBUGGINGusingSystem;usingSystem.Diagnostics;publicclassHelloWorld:Attribute{[Conditional(DEBUGGING)]publicvoidwriteHelloWorld(){Console.WriteLine(HelloWorld);Console.ReadLine();}publicstaticvoidMain(){HelloWorldhw=newHelloWorld();hw.writeHelloWorld();}}14.UsingInterfaces//接口代码usingSystem;interfaceIHelloWorld{voidwriteHelloWorld();}publicclassHelloWorld:IHelloWorld{publicvoidwriteHelloWorld(){Console.WriteLine(HelloWorld);}publicstaticvoidMain(){HelloWorldhw=newHelloWorld();hw.writeHelloWorld();}}15.DynamicHelloWorld//Again代码usingSystem;usingSystem.Reflection;namespaceHelloWorldNS{publicclassHelloWorld{publicstringwriteHelloWorld(){returnHelloWorld;}publicstaticvoidMain(string[]args){Typehw=Type.GetType(args[0]);//Instantiatingaclassdynamicallyobject[]nctorParams=newobject[]{};objectnobj=Activator.CreateInstance(hw,nctorParams);//Invokingamethodobject[]nmthdParams=newobject[]{};stringstrHelloWorld=(string)hw.InvokeMember(writeHelloWorld,BindingFlags.Default|BindingFlags.InvokeMethod,null,nobj,nmthdParams);Console.WriteLine(strHelloWorld);}}16.UnsafeHelloWorld//Unsafe代码usingSystem;publicclassHelloWorld{unsafepublicvoidwriteHelloWorld(char[]chrArray){fixed(char*parr=chrArray){char*pch=parr;for(inti=0;ichrArray.Length;i++)Console.Write(*(pch+i));}}publicstaticvoidMain(){HelloWorldhw=newHelloWorld();char[]chrHelloWorld=newchar[]{'H','e','l','l','o','','W','o','r','l','d'};hw.writeHelloWorld(chrHelloWorld);}17.UsingInteropServices代码usingSystem;usingSystem.Runtime.InteropServices;classClass1{//COM,API接口我以前就这么低俗地理解.其实工具会帮你生成[DllImport(kernel32)]privatestaticexternintBeep(intdwFreq,intdwDuration);staticvoidMain(string[]args){Console.WriteLine(HelloWorld);Beep(1000,2000);}}原文地址:://