标题:使用C#编写不同的"Hello World"程序
只看楼主
shan0yu
Rank: 1
等 级:新手上路
帖 子:110
专家分:0
注 册:2004-6-12
 问题点数:0 回复次数:19 
使用C#编写不同的"Hello World"程序

1. A Beginners Hello World public class HelloWorld { public static void Main() { System.Console.WriteLine("HELLO WORLD"); } } 2. Slightly improved version using System;

public class HelloWorld { public static void Main() { Console.WriteLine("HELLO WORLD"); } } 3. Command Line Arguments using System;

public class HelloWorld { public static void Main(string[] args) { Console.WriteLine(args[0]); } } 4. From Constructor using System; public class HelloWorld { public HelloWorld() { Console.WriteLine("HELLO WORLD"); }

public static void Main() { HelloWorld hw = new HelloWorld(); } } 5. More OO using System; public class HelloWorld { public void helloWorld() { Console.WriteLine("HELLO WORLD"); }

public static void Main() { HelloWorld hw = new HelloWorld(); hw.HelloWorld(); } } 6. From another class 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"); } } 7. Inheritance 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(); } } 8. Static Constructor 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(); } } 9. Exception Handling using System;

public class HelloWorld { public static void Main(string[] args) { try { Console.WriteLine(args[0]); } catch(IndexOutOfRangeException e) { Console.WriteLine(e.ToString()); } } } 10. Creating a DLL and using it in an application 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();

} } } 11. Using Property using System; public class HelloWorld { public string strHelloWorld { get { return "Hello World"; } }

public static void Main() { HelloWorld hw = new HelloWorld(); Console.WriteLine(cs.strHelloWorld); } } 12. Using Delegates using System; class HelloWorld { static void writeHelloWorld() { Console.WriteLine("HelloWorld"); } static void Main() { SimpleDelegate d = new SimpleDelegate(writeHelloWorld); d(); } } 13. Using Attributes #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(); } } 14. Using Interfaces 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(); } } 15. Dynamic Hello World 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);

// Invoking a method

object[] nmthdParams = new object[] {}; string strHelloWorld = (string) hw.InvokeMember( "writeHelloWorld", BindingFlags.Default | BindingFlags.InvokeMethod, null, nobj, nmthdParams);

Console.WriteLine(strHelloWorld); } } } 16. Unsafe Hello World 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); } } 17. Using InteropServices 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); } }

搜索更多相关主题的帖子: Hello World 编写 
2004-07-24 07:06
hwq
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2004-6-7
得分:0 

复杂!!!!!!!

为什么不做注释

2004-09-04 10:43
williamjack
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2004-6-17
得分:0 

楼主很厉害哦。每个方法都很好啊。。


…………………… 我有,我可以!!!
2004-09-04 14:27
ASP点儿NET
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2004-9-26
得分:0 

各有什么优缺点?哪种情况下用哪种语句好点儿呢?请详细说明下吧,我是新手,不明白


刚接触电脑,想学编程,学哪样呢?就.NET了... ...
2004-09-27 08:21
kgdiwss
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2004-9-26
得分:0 
我也是新手啊,写程序要养成注释的好习惯哦!
2004-10-09 18:34
leeteng
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2005-1-7
得分:0 
很多是错误的,而且这些代码太牵强了.看着玩还可以.没必要深究.
2005-01-11 11:17
Ericzhou
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2004-12-27
得分:0 
有点意思,呵呵

2005-01-11 16:35
goodgoodstudy
Rank: 2
等 级:新手上路
威 望:3
帖 子:111
专家分:0
注 册:2005-4-6
得分:0 
好厉害!这么多的方法都能想起来,这用来开发智力还可以的,只不过有点像回字有四种写法

2005-04-06 16:29
梦幻情缘
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:769
专家分:20
注 册:2005-4-4
得分:0 
我觉得没有这个必要,有时间还不如去多研究些更深层次的东西呢.
2005-04-06 19:51
ant3000
Rank: 1
等 级:新手上路
帖 子:188
专家分:0
注 册:2004-6-7
得分:0 
我想问一下 为什么学哪一种语言的第一句都是输出 Hello World 的~~  奇怪~~~
2005-04-06 21:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-3558-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.233018 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved