自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

不一樣的入門:看C# Hello World的17種寫法

開發(fā) 后端
本文針對不同階段、不同程度的C#學(xué)習(xí)者,介紹了C# Hello World的17種不同寫法,希望會對大家有所幫助。

C# Hello World寫法入門:

1. 初學(xué)者

  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. 構(gòu)造函數(shù)

  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. 面向?qū)ο?/P>

  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. 靜態(tài)構(gòu)造函數(shù)

  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. 動態(tài)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("writeHelloWorld", BindingFlags.Default | BindingFlags.InvokeMethod, null, nobj, nmthdParams);  
  26.  
  27. Console.WriteLine(strHelloWorld);   
  28. }   

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. }   

【編輯推薦】

  1. 配置C#命令行編譯器的步驟介紹
  2. C#連接數(shù)據(jù)庫的方法簡介
  3. 如何在C#添加鼠標(biāo)右鍵菜單
  4. .Net Framework中的委托與事件
  5. Observer設(shè)計模式范例詳解
責(zé)任編輯:book05 來源: hi.baidu
相關(guān)推薦

2012-03-07 17:24:10

戴爾咨詢

2012-12-20 10:17:32

IT運維

2016-05-09 18:40:26

VIP客戶緝拿

2017-05-25 15:02:46

聯(lián)宇益通SD-WAN

2015-10-19 12:33:01

華三/新IT

2018-05-09 15:42:24

新零售

2009-02-04 15:43:45

敏捷開發(fā)PHPFleaPHP

2009-12-01 16:42:27

Gentoo Linu

2009-06-12 15:26:02

2011-02-28 10:38:13

Windows 8

2016-03-24 18:51:40

2013-01-11 18:10:56

軟件

2015-08-25 09:52:36

云計算云計算產(chǎn)業(yè)云計算政策

2015-08-04 14:49:54

Discover

2009-07-07 10:44:14

多態(tài)

2019-01-03 14:39:08

Oracle甲骨文ORACLE

2022-05-05 21:47:32

Linuxls 命令

2009-11-26 13:16:25

Open Suse

2016-11-10 19:10:09

唯品會雙11

2020-03-25 16:07:38

網(wǎng)絡(luò)空間網(wǎng)絡(luò)空間安全網(wǎng)絡(luò)安全
點贊
收藏

51CTO技術(shù)棧公眾號