C# 程式的一般結構
C# 程式由一或多個檔案組成。 每個檔案含有零個或多個命名空間。 命名空間含有類別、結構、介面、列舉和委派等型別,或其他命名空間。 以下範例是 C# 程式的基本架構,其中包含上述所有元素。
// A skeleton of a C# program
using System;
// Your program starts here:
Console.WriteLine("Hello world!");
namespace YourNamespace
{
class YourClass
{
}
struct YourStruct
{
}
interface IYourInterface
{
}
delegate int YourDelegate();
enum YourEnum
{
}
namespace YourNestedNamespace
{
struct YourStruct
{
}
}
}
上述範例針對程式的進入點使用最上層陳述式。 您也可以建立名為 Main
的靜態方法,做為程式的進入點,如下列範例所示:
// A skeleton of a C# program
using System;
namespace YourNamespace
{
class YourClass
{
}
struct YourStruct
{
}
interface IYourInterface
{
}
delegate int YourDelegate();
enum YourEnum
{
}
namespace YourNestedNamespace
{
struct YourStruct
{
}
}
class Program
{
static void Main(string[] args)
{
//Your program starts here...
Console.WriteLine("Hello world!");
}
}
}
相關章節
您可以在基本概念指南的型別一節中了解這些程式元素:
C# 語言規格
意見反映
https://aka.ms/ContentUserFeedback。
即將推出:我們會在 2024 年淘汰 GitHub 問題,並以全新的意見反應系統取代並作為內容意見反應的渠道。 如需更多資訊,請參閱:提交及檢視以下的意見反映: