日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
不一樣的入門:看C#HelloWorld的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. 構(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#添加鼠標右鍵菜單
  4. .Net Framework中的委托與事件
  5. Observer設計模式范例詳解

標題名稱:不一樣的入門:看C#HelloWorld的17種寫法
標題網(wǎng)址:http://www.dlmjj.cn/article/dphdsij.html