不一样的入门:看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

软件

2019-01-03 14:39:08

Oracle甲骨文ORACLE

2022-05-05 21:47:32

Linuxls 命令

2015-08-04 14:49:54

Discover

2009-07-07 10:44:14

多态

2009-11-26 13:16:25

Open Suse

2009-12-24 20:05:48

WLAN无线部署无线安全H3C

2012-08-15 10:43:11

手机冲浪
点赞
收藏

51CTO技术栈公众号