现在的位置: 首页 > 综合 > 正文

Hello World的17种写法(C#)(转贴)

2013年09月07日 ⁄ 综合 ⁄ 共 4715字 ⁄ 字号 评论关闭

/******
前几天在网上看见个比较有趣的东东,发上来大家看看.
包含了C#的很多技术.

*/

C# Hello World写法入门:

    1. 初学者

  1. public class HelloWorld   
  2. {   
  3. public static void Main()   
  4. {   
  5. System.Console.WriteLine("HELLO WORLD");   
  6. }   

    2. 改进的HELLO WORLD

  1. using System;  
  2.  
  3. public class HelloWorld   
  4. {   
  5. public static void Main()   
  6. {   
  7. Console.WriteLine("HELLO WORLD");   
  8. }   

    3. 命令行形式

  1. using System;  
  2.  
  3. public class HelloWorld   
  4. {   
  5. public static void Main(string[] args)   
  6. {   
  7. Console.WriteLine(args[0]);   
  8. }   
  9. }  

    4. 构造函数

  1. using System;   
  2. public class HelloWorld   
  3. {   
  4. public HelloWorld()   
  5. {   
  6. Console.WriteLine("HELLO WORLD");   
  7. }  
  8.  
  9. public static void Main()   
  10. {   
  11. HelloWorld hw = new HelloWorld();   
  12. }   
  13. }  
  14.  

C# Hello World写法进阶:

    5. 面向对象

  1. using System;   
  2. public class HelloWorld   
  3. {   
  4. public void helloWorld()   
  5. {   
  6. Console.WriteLine("HELLO WORLD");   
  7. }  
  8.  
  9. public static void Main()   
  10. {   
  11. HelloWorld hw = new HelloWorld();   
  12. hw.HelloWorld();   
  13. }   

    6. 从其他类

  1. using System;   
  2. public class HelloWorld   
  3. {   
  4. public static void Main()   
  5. {   
  6. HelloWorldHelperClass hwh = new HelloWorldHelperClass();   
  7. hwh.writeHelloWorld();   
  8. }   
  9. }  
  10.  
  11. public class HelloWorldHelperClass   
  12. {   
  13. public void writeHelloWorld()   
  14. {   
  15. Console.WriteLine("Hello World");   
  16. }   

    7. 继承

  1. abstract class HelloWorldBase   
  2. {   
  3. public abstract void writeHelloWorld();   
  4. }   
  5. class HelloWorld : HelloWorldBase   
  6. {   
  7. public override void writeHelloWorld()   
  8. {   
  9. Console.WriteLine("Hello World");   
  10. }   
  11. }   
  12. class HelloWorldImp   
  13. {   
  14. static void Main() {   
  15. HelloWorldBase hwb = HelloWorld;   
  16. HelloWorldBase.writeHelloWorld();   
  17. }   

    8. 静态构造函数

  1. using System;   
  2. public class HelloWorld   
  3. {   
  4. private static string strHelloWorld;  
  5.  
  6. static HelloWorld()   
  7. {   
  8. strHelloWorld = "Hello World";   
  9. }   
  10. void writeHelloWorld()   
  11. {   
  12. Console.WriteLine(strHelloWorld);   
  13. }  
  14.  
  15. public static void Main()   
  16. {   
  17. HelloWorld hw = new HelloWorld();   
  18. hw.writeHelloWorld();   
  19. }   

    9. 异常处理

  1. using System;  
  2.  
  3. public class HelloWorld   
  4. {   
  5. public static void Main(string[] args)   
  6. {   
  7. try   
  8. {   
  9. Console.WriteLine(args[0]);   
  10. }   
  11. catch(IndexOutOfRangeException e)   
  12. {   
  13. Console.WriteLine(e.ToString());   
  14. }   
  15. }   

    10. 名字空间

  1. using System;  
  2.  
  3. namespace HelloLibrary   
  4. {   
  5. public class HelloMessage   
  6. {   
  7. public string Message   
  8. {   
  9. get   
  10. {   
  11. return "Hello, World!!!";   
  12. }   
  13. }   
  14. }   
  15. }   
  16. ------   
  17. using System;   
  18. using HelloLibrary;  
  19.  
  20. namespace HelloApplication   
  21. {   
  22. class HelloApp   
  23. {  
  24.  
  25. public static void Main(string[] args)   
  26. {   
  27. HelloMessage m = new HelloMessage();  
  28.  
  29. }   
  30. }   

    11. 属性

  1. using System;   
  2. public class HelloWorld   
  3. {   
  4. public string strHelloWorld   
  5. {   
  6. get   
  7. {   
  8. return "Hello World";   
  9. }   
  10. }  
  11.  
  12. public static void Main()   
  13. {   
  14. HelloWorld hw = new HelloWorld();   
  15. Console.WriteLine(cs.strHelloWorld);   
  16. }   

    12. 代理

  1. using System;   
  2. class HelloWorld   
  3. {   
  4. static void writeHelloWorld() {   
  5. Console.WriteLine("HelloWorld");   
  6. }   
  7. static void Main() {   
  8. SimpleDelegate d = new SimpleDelegate(writeHelloWorld);   
  9. d();   
  10. }   

    13. 使用属性

  1. #define DEBUGGING  
  2.  
  3. using System;   
  4. using System.Diagnostics;  
  5.  
  6. public class HelloWorld : Attribute   
  7. {   
  8. [Conditional("DEBUGGING")]  
  9.  
  10. public void writeHelloWorld()   
  11. {   
  12. Console.WriteLine("Hello World");   
  13. }  
  14.  
  15. public static void Main()   
  16. {   
  17. HelloWorld hw = new HelloWorld();  
  18.  
  19. hw.writeHelloWorld();   
  20. }   

    14. 接口

  1. using System;  
  2.  
  3. interface IHelloWorld   
  4. {   
  5. void writeHelloWorld();   
  6. }  
  7.  
  8. public class HelloWorld : IHelloWorld   
  9. {   
  10. public void writeHelloWorld()   
  11. {   
  12. Console.WriteLine("Hello World");   
  13. }  
  14.  
  15. public static void Main()   
  16. {   
  17. HelloWorld hw = new HelloWorld();  
  18.  
  19. hw.writeHelloWorld();   
  20. }   

C# Hello World的特别写法:

    15. 动态Hello World

  1. using System;   
  2. using System.Reflection;  
  3.  
  4. namespace HelloWorldNS{  
  5.  
  6. public class HelloWorld   
  7. {   
  8. public string writeHelloWorld()   
  9. {   
  10. return "HelloWorld";   
  11. }  
  12.  
  13. public static void Main(string[] args)   
  14. {   
  15. Type hw = Type.GetType(args[0]);  
  16.  
  17. // Instantiating a class dynamically  
  18.  
  19. object[] nctorParams = new object[] {};   
  20. object nobj = Activator.CreateInstance(hw, nctorParams);//, nctorParams);  
  21.  
  22. // Invoking a method  
  23.  
  24. object[] nmthdParams = new object[] {};   
  25. string strHelloWorld = (string) hw.InvokeMember(
  26. "writeHelloWorld"
  27. BindingFlags.Default | BindingFlags.InvokeMethod, 
  28. null
  29. nobj, 
  30. nmthdParams);  
  31.  
  32. Console.WriteLine(strHelloWorld);   
  33. }   

    16. 不安全代码Hello World

  1. using System;  
  2.  
  3. public class HelloWorld   
  4. {   
  5. unsafe public void writeHelloWorld(char[] chrArray)   
  6. {   
  7. fixed(char *parr = chrArray)   
  8. {   
  9. char *pch = parr;   
  10. for(int i=0; i< chrArray.Length; i++)   
  11. Console.Write(*(pch+i));   
  12. }   
  13. }  
  14.  
  15. public static void Main()   
  16. {   
  17. HelloWorld hw = new HelloWorld();   
  18. char[] chrHelloWorld = new char[] {'H','e','l','l','o'' ''W','o','r','l','d'};   
  19. hw.writeHelloWorld(chrHelloWorld);   
  20. }   

    17. 使用InteropServices

  1. using System;   
  2. using System.Runtime.InteropServices;  
  3.  
  4. class Class1   
  5. {   
  6. [DllImport("kernel32")]   
  7. private static extern int Beep(int dwFreq, int dwDuration);  
  8.  
  9. static void Main(string[] args)   
  10. {   
  11. Console.WriteLine("Hello World");   
  12. Beep(1000, 2000);   
  13. }   

 

【上篇】
【下篇】

抱歉!评论已关闭.