犬者
“说了你又不听,听又不懂,懂又不做,做又做错,错又不认,认又不改,改又不服,不服也不说,那叫我怎么办?!”

【电脑】Hello World in different styles using C#

好久没有去看code project的news letters了……心血来潮,去看了一下,发现这么篇好东西……估计对有兴趣学习C#的朋友会有帮助……
原文连接:http://www.codeproject.com/csharp/DifferentHelloWorlds.asp
作者:Wizard12

Introduction
I’ve attempted to write the traditional ‘Hello World’ in different styles. This explores the different possibilities of addressing a problem – ‘Hello World’ with different features of the C# language and the .NET framework.

The implementations
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        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);
  }
}
968
问天 @2/20/2004 9:58:19 PM
View blogs in this category:电脑


Please leave your comment here

 
  名字:
  主页:
  内容:
 

   


Navigation
Blogwind
犬者首页
Contact


个人档案


“说了你又不听,听又不懂,懂又不做,做又做错,错又不认,认又不改,改又不服,不服也不说,那叫我怎么办?!”



Categories
死结(27)
电脑(169)
心情(175)
天影(25)
乱弹(204)
博客(78)
音乐(18)
饕餮(30)
读书(19)
电影(26)
网摘(5)
希望(30)
汕头(10)
经济(5)
苹果(19)
跋涉(3)



Archive
2008年7月
2008年6月
2008年5月
2008年4月
2008年3月
2008年2月
2008年1月
2007年12月
2007年11月
2007年10月
2007年9月
2007年8月
2007年7月
2007年6月
2007年5月
2007年4月
2007年3月
2007年2月
2007年1月
2006年12月
2006年11月
2006年10月
2006年9月
2006年8月
2006年7月
2006年6月
2006年5月
2006年4月
2006年3月
2006年2月
2006年1月
2005年12月
2005年11月
2005年10月
2005年9月
2005年8月
2005年7月
2005年6月
2005年5月
2005年4月
2005年3月
2005年2月
2005年1月
2004年12月
2004年11月
2004年10月
2004年9月
2004年8月
2004年7月
2004年6月
2004年5月
2004年4月
2004年3月
2004年2月
2004年1月
2003年12月



My Links
bloglines
时尚摄影师奇科的博客
我们的漫画
颜如玉
最爱卫斯理

RSS 2.0

Username:
Password:
 Remember me