欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#學習進階Hello World的17種寫法代碼分享

 更新時間:2013年12月04日 09:17:10   作者:  
本文針對不同階段、不同程度的C#學習者,介紹了C# Hello World的17種不同寫法,C# Hello World寫法入門、C# Hello World寫法進階、C# Hello World的特別寫法三種角度進行推進

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. 構造函數(shù)

 

復制代碼 代碼如下:

 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. 靜態(tài)構造函數(shù)

復制代碼 代碼如下:

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. 動態(tài)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);
    }
}

17. 使用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);
    }
}

相關文章

  • Unity Shader實現(xiàn)素描風格的渲染

    Unity Shader實現(xiàn)素描風格的渲染

    這篇文章主要為大家詳細介紹了Unity Shader實現(xiàn)素描風格的渲染,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#設計模式之外觀模式介紹

    C#設計模式之外觀模式介紹

    外觀模式:為子系統(tǒng)中的一組接口提供一個一致的界面,此模式定義了一個高層的接口,這個借口使得這子系統(tǒng)容易使用
    2012-10-10
  • c#操作Redis的5種基本類型匯總

    c#操作Redis的5種基本類型匯總

    這篇文章主要給大家介紹了關于c#操作Redis的5種基本類型,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用C#具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2020-07-07
  • C#實現(xiàn)注冊碼注冊機制效果詳解

    C#實現(xiàn)注冊碼注冊機制效果詳解

    這篇文章主要為大家詳細介紹了C#如何實現(xiàn)注冊碼注冊機制效果,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-01-01
  • c# 代碼調(diào)試技巧和如何遠程調(diào)試

    c# 代碼調(diào)試技巧和如何遠程調(diào)試

    這篇文章主要介紹了c# 代碼調(diào)試技巧和如何遠程調(diào)試,幫助大家更好的理解和使用c#編程語言,感興趣的朋友可以了解下
    2020-11-11
  • C#日期轉換函數(shù)分享

    C#日期轉換函數(shù)分享

    這篇文章介紹了C#日期轉換函數(shù),有需要的朋友可以參考一下
    2013-10-10
  • c#模擬平拋運動動畫的方法詳解

    c#模擬平拋運動動畫的方法詳解

    本篇文章是對使用c#模擬平拋運動動畫的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • C#默認雙緩沖技術實例分析

    C#默認雙緩沖技術實例分析

    這篇文章主要介紹了C#默認雙緩沖技術,實例分析了雙緩沖技術的設置與實現(xiàn)技巧,需要的朋友可以參考下
    2015-06-06
  • C#連接MySQL的兩個簡單代碼示例

    C#連接MySQL的兩個簡單代碼示例

    這篇文章主要介紹了C#連接MySQL的簡單代碼示例,需要的朋友可以參考下
    2017-06-06
  • c# Process.Start()找不到系統(tǒng)文件的解決方法

    c# Process.Start()找不到系統(tǒng)文件的解決方法

    vs1027在X64應用程序下執(zhí)行process.start()時,OK;但是在X86應用程序下執(zhí)行process.start(),報錯:找不到系統(tǒng)文件,本文就詳細的介紹一下解決方法,感興趣的可以了解一下
    2023-09-09

最新評論