成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

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("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#連接數據庫的方法簡介
  3. 如何在C#添加鼠標右鍵菜單
  4. .Net Framework中的委托與事件
  5. Observer設計模式范例詳解
責任編輯:book05 來源: hi.baidu
相關推薦

2012-03-07 17:24:10

戴爾咨詢

2012-12-20 10:17:32

IT運維

2017-05-25 15:02:46

聯宇益通SD-WAN

2016-05-09 18:40:26

VIP客戶緝拿

2015-10-19 12:33:01

華三/新IT

2018-05-09 15:42:24

新零售

2009-02-04 15:43:45

敏捷開發PHPFleaPHP

2009-12-01 16:42:27

Gentoo Linu

2011-02-28 10:38:13

Windows 8

2009-06-12 15:26:02

2016-03-24 18:51:40

2015-08-25 09:52:36

云計算云計算產業云計算政策

2013-01-11 18:10:56

軟件

2015-08-04 14:49:54

Discover

2009-07-07 10:44:14

多態

2022-05-05 21:47:32

Linuxls 命令

2019-01-03 14:39:08

Oracle甲骨文ORACLE

2009-11-26 13:16:25

Open Suse

2021-02-01 06:10:02

springaop機制開發

2016-11-10 19:10:09

唯品會雙11
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲免费人成在线视频观看 | 中文字幕亚洲一区二区三区 | h视频网站在线观看 | 亚洲精品在线观看网站 | 日本一区二区高清不卡 | 日韩爱爱网 | 国产精品永久久久久 | 一区精品视频在线观看 | 国产精品亚洲综合 | 久久久国产一区 | 久久国产成人午夜av影院武则天 | 色婷婷精品国产一区二区三区 | 中文在线日韩 | 亚洲免费视频一区二区 | 国产精品一区久久久 | 97精品超碰一区二区三区 | 日本色婷婷 | 午夜国产 | 国产伦精品一区二区三区四区视频 | 欧美综合国产精品久久丁香 | 国产欧美久久精品 | 久久久久久久网 | 国产精品福利在线观看 | 国产免费一区二区 | 亚洲一区二区三区免费在线观看 | 成人欧美一区二区三区黑人孕妇 | 久久精品国产99国产精品 | 91人人澡人人爽 | 亚洲欧美视频 | 精品视频一区二区 | 五月激情六月婷婷 | 国产精品久久久久久238 | 欧美xxxⅹ性欧美大片 | 亚洲精品小视频在线观看 | 在线成人av| 国产一级黄色网 | 欧美午夜精品 | 国产精品久久久久久久久久久久久久 | 久久久国产精品视频 | 99爱国产| 日韩成人免费视频 |