https://www.cnblogs.com/jara/p/3456672.html
摘要:本文针对不同阶段、不同程度的C#学习者,介绍了C# Hello World的17种不同写法,希望会对大家有所帮助。(C# Hello World写法入门、C# Hello World写法进阶、C# Hello World的特别写法三种角度进行推进),本人觉得非常有趣,有兴趣的可以好好学习下。
C# Hello World写法入门:
1. 初学者
![复制代码](https://common.cnblogs.com/images/copycode.gif)
public class HelloWorld { public static void Main() { System.Console.WriteLine("HELLO WORLD"); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
2. 改进的HELLO WORLD
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; public class HelloWorld { public static void Main() { Console.WriteLine("HELLO WORLD"); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
3. 命令行形式
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; public class HelloWorld { public static void Main(string[] args) { Console.WriteLine(args[0]); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
4. 构造函数
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; public class HelloWorld { public HelloWorld() { Console.WriteLine("HELLO WORLD"); } public static void Main() { HelloWorld hw = new HelloWorld(); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
C# Hello World写法进阶:
5. 面向对象
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; public class HelloWorld { public void helloWorld() { Console.WriteLine("HELLO WORLD"); } public static void Main() { HelloWorld hw = new HelloWorld(); hw.HelloWorld(); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
6. 从其他类
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; public class HelloWorld { public static void Main() { HelloWorldHelperClass hwh = new HelloWorldHelperClass(); hwh.writeHelloWorld(); } } public class HelloWorldHelperClass { public void writeHelloWorld() { Console.WriteLine("Hello World"); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
7. 继承
![复制代码](https://common.cnblogs.com/images/copycode.gif)
abstract class HelloWorldBase { public abstract void writeHelloWorld(); } class HelloWorld : HelloWorldBase { public override void writeHelloWorld() { Console.WriteLine("Hello World"); } } class HelloWorldImp { static void Main() { HelloWorldBase hwb = HelloWorld; HelloWorldBase.writeHelloWorld(); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
8. 静态构造函数
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; public class HelloWorld { private static string strHelloWorld; static HelloWorld() { strHelloWorld = "Hello World"; } void writeHelloWorld() { Console.WriteLine(strHelloWorld); } public static void Main() { HelloWorld hw = new HelloWorld(); hw.writeHelloWorld(); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
9. 异常处理
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; public class HelloWorld { public static void Main(string[] args) { try { Console.WriteLine(args[0]); } catch (IndexOutOfRangeException e) { Console.WriteLine(e.ToString()); } } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
10. 名字空间
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; namespace HelloLibrary { public class HelloMessage { public string Message { get { return "Hello, World!!!"; } } } } //------ using System; using HelloLibrary; namespace HelloApplication { class HelloApp { public static void Main(string[] args) { HelloMessage m = new HelloMessage(); } } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
11. 属性
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; public class HelloWorld { public string strHelloWorld { get { return "Hello World"; } } public static void Main() { HelloWorld hw = new HelloWorld(); Console.WriteLine(cs.strHelloWorld); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
12. 代理
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; class HelloWorld { static void writeHelloWorld() { Console.WriteLine("HelloWorld"); } static void Main() { SimpleDelegate d = new SimpleDelegate(writeHelloWorld); d(); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
13. 使用属性
![复制代码](https://common.cnblogs.com/images/copycode.gif)
#define DEBUGGING using System; using System.Diagnostics; public class HelloWorld : Attribute { [Conditional("DEBUGGING")] public void writeHelloWorld() { Console.WriteLine("Hello World"); } public static void Main() { HelloWorld hw = new HelloWorld(); hw.writeHelloWorld(); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
14. 接口
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; interface IHelloWorld { void writeHelloWorld(); } public class HelloWorld : IHelloWorld { public void writeHelloWorld() { Console.WriteLine("Hello World"); } public static void Main() { HelloWorld hw = new HelloWorld(); hw.writeHelloWorld(); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
C# Hello World的特别写法:
15. 动态Hello World
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; using System.Reflection; namespace HelloWorldNS { public class HelloWorld { public string writeHelloWorld() { return "HelloWorld"; } public static void Main(string[] args) { Type hw = Type.GetType(args[0]); // Instantiating a class dynamically object[] nctorParams = new object[] { }; object nobj = Activator.CreateInstance(hw, nctorParams);//, nctorParams); // Invoking a method object[] nmthdParams = new object[] { }; string strHelloWorld = (string)hw.InvokeMember("writeHelloWorld", BindingFlags.Default | BindingFlags.InvokeMethod, null, nobj, nmthdParams); Console.WriteLine(strHelloWorld); } } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
16. 不安全代码Hello World
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; public class HelloWorld { unsafe public void writeHelloWorld(char[] chrArray) { fixed (char* parr = chrArray) { char* pch = parr; for (int i = 0; i < chrArray.Length; i++) Console.Write(*(pch + i)); } } public static void Main() { HelloWorld hw = new HelloWorld(); char[] chrHelloWorld = new char[] { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' }; hw.writeHelloWorld(chrHelloWorld); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
17. 使用InteropServices
![复制代码](https://common.cnblogs.com/images/copycode.gif)
using System; using System.Runtime.InteropServices; class Class1 { [DllImport("kernel32")] private static extern int Beep(int dwFreq, int dwDuration); static void Main(string[] args) { Console.WriteLine("Hello World"); Beep(1000, 2000); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
个人主要研究:金融系统、MIS系统、人力资源管理系统、数据采集系统、权限管理系统等等系统。主攻C#开发语言,Oracle、Sql Server,WCF和Remoting通信。
如需联系可加QQ:442389681 Email:[email protected] 手机:18922735098
QQ群交流:186841119 (请注明来自博客园)
博客园地址:http://www.cnblogs.com/jara/ http://www.cnblogs.com/luoyuhao/
提示:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果觉得还有帮助的话,可以点一下右下角的【推荐】,希望能够持续的为大家带来好的技术文章!想跟我一起进步么?那就【关注】我吧。
如果对文章有任何问题,都可以在评论中留言,我会尽可能的答复您,谢谢您的阅读