`
f002489
  • 浏览: 264459 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

不一样的入门:看C# Hello World的17种写法

    博客分类:
  • C#
 
阅读更多
http://developer.51cto.com/art/200908/145356.htm


C# Hello World写法入门:

1. 初学者

    public class HelloWorld  
    {  
    public static void Main()  
    {  
    System.Console.WriteLine("HELLO WORLD");  
    }  
    }

2. 改进的HELLO WORLD

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

3. 命令行形式

    using System; 
    
    public class HelloWorld  
    {  
    public static void Main(string[] args)  
    {  
    Console.WriteLine(args[0]);  
    }  
    } 

4. 构造函数

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

C# Hello World写法进阶:

5. 面向对象

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

6. 从其他类

    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. 继承

    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. 静态构造函数

    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. 异常处理

    using System; 
    
    public class HelloWorld  
    {  
    public static void Main(string[] args)  
    {  
    try  
    {  
    Console.WriteLine(args[0]);  
    }  
    catch(IndexOutOfRangeException e)  
    {  
    Console.WriteLine(e.ToString());  
    }  
    }  
    }

10. 名字空间

    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 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 System;  
    class HelloWorld  
    {  
    static void writeHelloWorld() {  
    Console.WriteLine("HelloWorld");  
    }  
    static void Main() {  
    SimpleDelegate d = new SimpleDelegate(writeHelloWorld);  
    d();  
    }  
    }

13. 使用属性

    #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 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();  
    }  
    }

C# Hello World的特别写法:

15. 动态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);//, 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. 不安全代码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);  
    }  
    }
分享到:
评论

相关推荐

    C#的17种Hello World的写法

    适合新手看的c#17种helloworld写法,让新手从简单的helloworld熟悉继承,接口,属性,等方法 从浅到深,很适合C#新手入门学习

    C#学习进阶Hello World的17种写法代码分享

    本文针对不同阶段、不同程度的C#学习者,介绍了C# Hello World的17种不同写法,C# Hello World写法入门、C# Hello World写法进阶、C# Hello World的特别写法三种角度进行推进

    hello world 的 N 种写法

    hello world 的 N 种写法 包含 批处理 汇编 vbs js c++ c# java boo 等近100种语言的HelloWorld

    WCF的几种HelloWorld写法研究

    博客源代码,本人初学WCF的HelloWorld学习例子,博客还在修改之中。博客地址 http://blog.csdn.net/pfe_Nova

    C#-Hello,Wrold

    C# Hello, World的17种写法,欢迎大家批评指正。

    梦幻was读取.zip

    c#读取梦幻西游was数据,最简单的写法, 看得懂hello,world.就看的懂这个源码.

    PHP调用.NET的WebService 简单实例

    public string HelloWorld() {  return “Hello World”; } ok,一切就绪。在某php文件中如下写法: php5本身就支持SOAP调用Web Service: &lt;?php //get localization strings from C# webs

    C#多线程编程中的锁系统基本用法

    平常在多线程开发中,总避免不了线程同步。... 目录 一:lock、Monitor  1:基础。  2: 作用域。  3:字符串锁。... 4:monitor使用 ...Lock是Monitor语法糖简化写法。... string obj = “helloworld”;  lock (obj)  

    PHP中调用ASP.NET的WebService的代码

    其中有个web method像这样的: 复制代码 代码如下: [WebMethod] public string HelloWorld() { return “Hello World”; } ok,一切就绪。在某php文件中如下写法: php5本身就支持SOAP调用Web Service: 复制代码 ...

    JavaScript中的this到底是什么(一)

    首先,Hello World o(^▽^)o 本人从刚开始自学javascript这门灵活的编程语言到现在,也有一年多的时间了。 在这一年多的时间里,这门语言所带给我的不仅是高额的工作回报,还有很多意想不到的惊喜,让我这样一个...

Global site tag (gtag.js) - Google Analytics