1. A Beginners Hello World 初学者
代码
public
class
HelloWorld
{
public static void Main()
{
System.Console.WriteLine( " HELLO WORLD " );
}
}
{
public static void Main()
{
System.Console.WriteLine( " HELLO WORLD " );
}
}
2. Slightly improved version 略有提高
代码
3. Command Line Arguments
命令行参数
using
System; (就这?会用命名空间?)
public class HelloWorld
{
public static void Main()
{
Console.WriteLine( " HELLO WORLD " );
}
}
public class HelloWorld
{
public static void Main()
{
Console.WriteLine( " HELLO WORLD " );
}
}
代码
4. From Constructor
构造函数
using
System;
public class HelloWorld
{
public static void Main( string [] args) // 会传参数了
{
Console.WriteLine(args[ 0 ]);
}
}
public class HelloWorld
{
public static void Main( string [] args) // 会传参数了
{
Console.WriteLine(args[ 0 ]);
}
}
代码
5. More OO
using
System;
public class HelloWorld
{
public HelloWorld()
{
Console.WriteLine( " HELLO WORLD " );
}
public static void Main()
{
HelloWorld hw = new HelloWorld(); // 会用类了?构造?
}
}
public class HelloWorld
{
public HelloWorld()
{
Console.WriteLine( " HELLO WORLD " );
}
public static void Main()
{
HelloWorld hw = new HelloWorld(); // 会用类了?构造?
}
}
代码
6. From another class
调用另一个类
using
System;
public class HelloWorld
{
public void helloWorld()
{
Console.WriteLine( " HELLO WORLD " );
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.HelloWorld(); // 更进一步的面向对象?会用方法了?
}
}
public class HelloWorld
{
public void helloWorld()
{
Console.WriteLine( " HELLO WORLD " );
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.HelloWorld(); // 更进一步的面向对象?会用方法了?
}
}
代码
7. Inheritance
继承
using
System;
public class HelloWorld
{
public static void Main()
{
HelloWorldHelperClass hwh = new HelloWorldHelperClass(); // 类里调用其它类?
hwh.writeHelloWorld();
}
}
public class HelloWorldHelperClass
{
public void writeHelloWorld()
{
Console.WriteLine( " Hello World " );
}
}
public class HelloWorld
{
public static void Main()
{
HelloWorldHelperClass hwh = new HelloWorldHelperClass(); // 类里调用其它类?
hwh.writeHelloWorld();
}
}
public class HelloWorldHelperClass
{
public void writeHelloWorld()
{
Console.WriteLine( " Hello World " );
}
}
代码
abstract
class
HelloWorldBase
//
抽象类
{
public abstract void writeHelloWorld();
}
class HelloWorld : HelloWorldBase // 继承----不得不严肃起来了,能抽象的已经可以做系统架构设计了!
{
public override void writeHelloWorld()
{
Console.WriteLine( " Hello World " );
Console.ReadLine();
}
}
class HelloWorldImp
{
static void Main()
{
HelloWorldBase hwb = new HelloWorld();
hwb.writeHelloWorld();
}
}
{
public abstract void writeHelloWorld();
}
class HelloWorld : HelloWorldBase // 继承----不得不严肃起来了,能抽象的已经可以做系统架构设计了!
{
public override void writeHelloWorld()
{
Console.WriteLine( " Hello World " );
Console.ReadLine();
}
}
class HelloWorldImp
{
static void Main()
{
HelloWorldBase hwb = new HelloWorld();
hwb.writeHelloWorld();
}
}
8. Static Constructor 静态构造函数
代码
9. Exception Handling
异常处理
using
System;
public class HelloWorld
{
private static string strHelloWorld;
static HelloWorld() // 静态构造
{
strHelloWorld = " Hello World " ;
}
void writeHelloWorld()
{
Console.WriteLine(strHelloWorld); }
public static void Main()
{
HelloWorld hw = new HelloWorld(); // 需要吗?
hw.writeHelloWorld(); // 平常我会觉得很可笑----居然写得这么啰嗦
}
}
public class HelloWorld
{
private static string strHelloWorld;
static HelloWorld() // 静态构造
{
strHelloWorld = " Hello World " ;
}
void writeHelloWorld()
{
Console.WriteLine(strHelloWorld); }
public static void Main()
{
HelloWorld hw = new HelloWorld(); // 需要吗?
hw.writeHelloWorld(); // 平常我会觉得很可笑----居然写得这么啰嗦
}
}
代码
using
System;
public class HelloWorld
{
public static void Main( string [] args)
{
try
{
Console.WriteLine(args[ 0 ]);
}
catch (IndexOutOfRangeException e) // 会用异常处理了,但如何更好回收资源呢?异常接下来应该是资源回收啊?我以前也犯这种毛病,GC应该怎么更好使用,我到现在还不是很纯熟
{
Console.WriteLine(e.ToString());
}
}
public class HelloWorld
{
public static void Main( string [] args)
{
try
{
Console.WriteLine(args[ 0 ]);
}
catch (IndexOutOfRangeException e) // 会用异常处理了,但如何更好回收资源呢?异常接下来应该是资源回收啊?我以前也犯这种毛病,GC应该怎么更好使用,我到现在还不是很纯熟
{
Console.WriteLine(e.ToString());
}
}
10. Creating a DLL and using it in an application 做组件吗?
代码
using
System;
namespace HelloLibrary
{
public class HelloMessage
{
public string Message
{
get
{
return " Hello, World!!! " ;
}
}
}
}
namespace HelloLibrary
{
public class HelloMessage
{
public string Message
{
get
{
return " Hello, World!!! " ;
}
}
}
}
//------
代码
using
System;
using HelloLibrary;
namespace HelloApplication
{
class HelloApp
{
public static void Main( string [] args)
{
HelloMessage m = new HelloMessage();
}
}
}
using HelloLibrary;
namespace HelloApplication
{
class HelloApp
{
public static void Main( string [] args)
{
HelloMessage m = new HelloMessage();
}
}
}
11. Using Property 实用属性
代码
using
System;
public class HelloWorld
{
public string strHelloWorld
{
get // 会用属性了
{
return " Hello World " ;
}
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
Console.WriteLine(hw.strHelloWorld);
}
}
public class HelloWorld
{
public string strHelloWorld
{
get // 会用属性了
{
return " Hello World " ;
}
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
Console.WriteLine(hw.strHelloWorld);
}
}
12. Using Delegates //委托!
代码
13. Using Attributes //我不会!补习去!
using
System;
class HelloWorld
{
delegate void SimpleDelegate(); // 定义委托
static void writeHelloWorld()
{
Console.WriteLine( " HelloWorld " );
}
static void Main()
{
SimpleDelegate d = new SimpleDelegate(writeHelloWorld); // 委托?!?!
d(); // 语法的确这么写,但含义无法理解;因为实在体会不出好处来
}
}
class HelloWorld
{
delegate void SimpleDelegate(); // 定义委托
static void writeHelloWorld()
{
Console.WriteLine( " HelloWorld " );
}
static void Main()
{
SimpleDelegate d = new SimpleDelegate(writeHelloWorld); // 委托?!?!
d(); // 语法的确这么写,但含义无法理解;因为实在体会不出好处来
}
}
代码
14. Using Interfaces
//接口
#define
DEBUGGING
using System;
using System.Diagnostics;
public class HelloWorld : Attribute
{
[Conditional( " DEBUGGING " )]
public void writeHelloWorld()
{
Console.WriteLine( " Hello World " );
Console.ReadLine();
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.writeHelloWorld();
}
}
using System;
using System.Diagnostics;
public class HelloWorld : Attribute
{
[Conditional( " DEBUGGING " )]
public void writeHelloWorld()
{
Console.WriteLine( " Hello World " );
Console.ReadLine();
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.writeHelloWorld();
}
}
代码
15. Dynamic Hello World //Again
using
System;
interface IHelloWorld
{
void writeHelloWorld();
}
public class HelloWorld : IHelloWorld
{
public void writeHelloWorld()
{
Console.WriteLine( " Hello World " );
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.writeHelloWorld();
}
}
interface IHelloWorld
{
void writeHelloWorld();
}
public class HelloWorld : IHelloWorld
{
public void writeHelloWorld()
{
Console.WriteLine( " Hello World " );
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.writeHelloWorld();
}
}
代码
16. Unsafe Hello World
//
Unsafe
using
System;
using System.Reflection;
namespace HelloWorldNS
{
public class HelloWorld
{
public string writeHelloWorld()
{
return " HelloWorld " ;
}
public static void Main( string [] args)
{
Type hw = Type.GetType(args[ 0 ]);
// Instantiating a class dynamically
object [] nctorParams = new object [] {};
object nobj = Activator.CreateInstance(hw,
nctorParams);
// Invoking a method
object [] nmthdParams = new object [] {};
string strHelloWorld = ( string ) hw.InvokeMember(
" writeHelloWorld " , BindingFlags.Default |
BindingFlags.InvokeMethod, null ,
nobj, nmthdParams);
Console.WriteLine(strHelloWorld);
}
}
using System.Reflection;
namespace HelloWorldNS
{
public class HelloWorld
{
public string writeHelloWorld()
{
return " HelloWorld " ;
}
public static void Main( string [] args)
{
Type hw = Type.GetType(args[ 0 ]);
// Instantiating a class dynamically
object [] nctorParams = new object [] {};
object nobj = Activator.CreateInstance(hw,
nctorParams);
// Invoking a method
object [] nmthdParams = new object [] {};
string strHelloWorld = ( string ) hw.InvokeMember(
" writeHelloWorld " , BindingFlags.Default |
BindingFlags.InvokeMethod, null ,
nobj, nmthdParams);
Console.WriteLine(strHelloWorld);
}
}
代码
using
System;
public class HelloWorld
{
unsafe public void writeHelloWorld( char [] chrArray)
{
fixed ( char * parr = chrArray)
{
char * pch = parr;
for ( int i = 0 ; i < chrArray.Length; i ++ )
Console.Write( * (pch + i));
}
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
char [] chrHelloWorld = new char [] { ' H ' , ' e ' , ' l ' , ' l ' , ' o ' , ' ' , ' W ' , ' o ' , ' r ' , ' l ' , ' d ' };
hw.writeHelloWorld(chrHelloWorld);
}
public class HelloWorld
{
unsafe public void writeHelloWorld( char [] chrArray)
{
fixed ( char * parr = chrArray)
{
char * pch = parr;
for ( int i = 0 ; i < chrArray.Length; i ++ )
Console.Write( * (pch + i));
}
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
char [] chrHelloWorld = new char [] { ' H ' , ' e ' , ' l ' , ' l ' , ' o ' , ' ' , ' W ' , ' o ' , ' r ' , ' l ' , ' d ' };
hw.writeHelloWorld(chrHelloWorld);
}
17. Using InteropServices
代码
using
System;
using System.Runtime.InteropServices;
class Class1
{ // COM , API接口 我以前就这么低俗地理解. 其实工具会帮你生成
[DllImport( " kernel32 " )]
private static extern int Beep( int dwFreq, int dwDuration);
static void Main( string [] args)
{
Console.WriteLine( " Hello World " );
Beep( 1000 , 2000 );
}
}
using System.Runtime.InteropServices;
class Class1
{ // COM , API接口 我以前就这么低俗地理解. 其实工具会帮你生成
[DllImport( " kernel32 " )]
private static extern int Beep( int dwFreq, int dwDuration);
static void Main( string [] args)
{
Console.WriteLine( " Hello World " );
Beep( 1000 , 2000 );
}
}
原文地址:http://www.cnblogs.com/babyblue/archive/2004/03/11/2852.html