讓我們來撰寫第一個C#程式 Hello World!

首先建立一個名為Hello1的類別

 

範例一

方法一:

public class Hello1

{

public static void Main()

{

System.Console.WriteLine("Hello, World!");   //" "為輸出的內容

}

}

輸出結果

Hello,World!

 

方法二:

using System

 

public class Hello1

{

public static void Main()

{

Console.WriteLine("Hello, World!");

}

輸出結果:

Hello,World!

在開頭前面加入using System,可免去內容一直重複撰寫system

 

範例二

如果您想要存取傳遞到您應用程式的命令列參數,只需變更 Main 方法的簽名碼,以下列方式來包含它們。本範例計算和顯示命令列引數。

// Hello3.cs

// arguments: A B C D

using System;

public class Hello1

{

public static void Main(string[] args)     //使用參數

{

Console.WriteLine("Hello, World!");

Console.WriteLine("You entered the following {0} command line arguments:", args.Length );

for (int i=0; i < args.Length; i++)

{

Console.WriteLine("{0}", args[i]);

}

}

}

輸出結果:

Hello, World!

You entered the following 4 command line arguments:

A

B

C

D

 

若要傳回一個傳回碼 (Return Code),請將 Main 方法的簽名碼變更成:
using System;

public class Hello1

{

public static int Main(string[] args)

{

Console.WriteLine("Hello, World!");

return 0;

}

}

輸出結果:

Hello, World!

 

資料來源:http://msdn.microsoft.com/zh-tw/library/aa288463(v=vs.71).aspx

arrow
arrow
    全站熱搜

    東勢厝滴yang 發表在 痞客邦 留言(0) 人氣()