事件
C# 程式的一般結構
C# 程式包含一或多個檔案。 每個檔案都包含零個或多個命名空間。 命名空間包含類別、結構、介面、列舉和委派或其他命名空間等類型。 下列範例是 C# 程式的基本架構,其中包含所有這些元素。
using System;
Console.WriteLine("Hello world!");
namespace YourNamespace
{
class YourClass
{
}
struct YourStruct
{
}
interface IYourInterface
{
}
delegate int YourDelegate();
enum YourEnum
{
}
namespace YourNestedNamespace
{
struct YourStruct
{
}
}
}
上述範例會針對程序的進入點使用 最上層語句。 只有一個檔案可以有最上層語句。 程序的進入點是該檔案中的第一行程式文字。 在這種情況下,它是 Console.WriteLine("Hello world!");
。
您也可以建立名為 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)
{
Console.WriteLine("Hello world!");
}
}
}
在此情況下,程式會從 Main
方法的第一行開始,這是 Console.WriteLine("Hello world!");
您會在基本概念指南的 類型 一節中了解這些程式元素:
如需詳細資訊,請參閱 C# 語言規格中 的基本概念。 語言規格是 C# 語法和使用方式的最終來源。
其他資源
訓練
模組
Write your first C# code - Training
Get started by writing code examples to learn the basics of the C# syntax.
文件
-
C# docs - get started, tutorials, reference.
Learn C# programming - for beginning developers, developers new to C#, and experienced C# / .NET developers
-
New to C#? Learn the basics of the language. Start with this overview.
-
Learn the fundamentals of the C# type system - C#
Learn about creating types in C#, such as tuples, records, value types, and reference types. Learn to choose between these options.