a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

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

2020-01-24 03:02:11
字體:
來源:轉載
供稿:網友

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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日本免费黄色网址 | 日本久久精品 | 久久av影视 | 成人国产免费视频 | 亚洲国产精品久久久久 | 日本午夜网 | 一区二区三区国产精品 | 6080yy精品一区二区三区 | 久久精品成人av | 亚洲精品午夜aaa久久久 | 日韩性网站 | 国产精品一区二区在线看 | 国产成人高清视频 | 91污在线 | 91精品福利 | 亚洲免费婷婷 | 亚洲一区二区三区在线免费观看 | 亚洲va中文字幕 | 国产中文一区二区三区 | 91一区二区在线 | 日本高清视频网站www | 成人在线小视频 | 欧美成人免费在线视频 | 国产精品久久一区 | 日本精品一区二区三区在线观看视频 | 久久国产精品毛片 | 一级毛片网| 狠狠操中文字幕 | 欧美午夜精品久久久久免费视 | 欧美成人性生活 | 大陆一级毛片免费看 | 中文二区 | 日韩中文字幕在线看 | 一级黄色录象片 | 亚洲欧洲在线观看 | 免费看的毛片 | 男人的天堂视频网站 | 国产亚洲一区二区不卡 | 国产一区二区三区免费视频 | 一区二区日韩 | 欧美激情一区二区三区四区 |