程序结构Program Structure

C# 中的关键组织结构概念包括程序命名空间类型成员程序集The key organizational concepts in C# are programs, namespaces, types, members, and assemblies. C# 程序由一个或多个源文件组成。C# programs consist of one or more source files. 程序声明类型,而类型则包含成员,并被整理到命名空间中。Programs declare types, which contain members and can be organized into namespaces. 类型示例包括类和接口。Classes and interfaces are examples of types. 成员示例包括字段、方法、属性和事件。Fields, methods, properties, and events are examples of members. 编译完的 C# 程序实际上会打包到程序集中。When C# programs are compiled, they are physically packaged into assemblies. 程序集的文件扩展名通常为 .exe.dll,具体取决于实现的是应用程序还是Assemblies typically have the file extension .exe or .dll, depending on whether they implement applications or libraries, respectively.

以下示例在 Acme.Collections 命名空间中声明 Stack 类:The example declares a class named Stack in a namespace called Acme.Collections:

using System;
namespace Acme.Collections
{
    public class Stack
    {
        Entry top;
        public void Push(object data) 
        {
            top = new Entry(top, data);
        }

        public object Pop() 
        {
            if (top == null)
            {
                throw new InvalidOperationException();
            }
            object result = top.data;
            top = top.next;
            return result;
        }
        
        class Entry
        {
            public Entry next;
            public object data;
            public Entry(Entry next, object data)
            {
                this.next = next;
                this.data = data;
            }
        }
    }
}

此类的完全限定的名称为 Acme.Collections.StackThe fully qualified name of this class is Acme.Collections.Stack. 此类包含多个成员:一个 top 字段、两个方法(PushPop)和一个 Entry 嵌套类。The class contains several members: a field named top, two methods named Push and Pop, and a nested class named Entry. Entry 类还包含三个成员:一个 next 字段、一个 data 字段和一个构造函数。The Entry class further contains three members: a field named next, a field named data, and a constructor. 假定示例的源代码存储在 acme.cs 文件中,以下命令行Assuming that the source code of the example is stored in the file acme.cs, the command line

csc /t:library acme.cs

将示例编译成库(不含 Main 入口点的代码),并生成 acme.dll 程序集。compiles the example as a library (code without a Main entry point) and produces an assembly named acme.dll.

重要

上述示例使用 csc 作为命令行 C# 编译器。The examples above use csc as the command line C# compiler. 此编译器是 Windows 可执行文件。This compiler is a Windows executable. 若要在其他平台上使用 C#,应使用 .NET Core 工具。To use C# across other platforms, you should use the tools for .NET Core. .NET Core 生态系统使用 dotnet CLI 来管理命令行生成。The .NET Core ecosystem uses the dotnet CLI to manage command line builds. 这包括管理依赖项和调用 C# 编译器。This includes managing dependencies, and invoking the C# compiler. 有关在 .NET Core 支持的平台上使用这些工具的完整说明,请参阅这篇教程See this tutorial for a full description of those tools on the platforms supported by .NET Core.

程序集包含中间语言 (IL) 指令形式的可执行代码和元数据形式的符号信息。Assemblies contain executable code in the form of Intermediate Language (IL) instructions, and symbolic information in the form of metadata. 执行前,程序集中的 IL 代码会被 .NET 公共语言运行时的实时 (JIT) 编译器自动转换成处理器专属代码。Before it is executed, the IL code in an assembly is automatically converted to processor-specific code by the Just-In-Time (JIT) compiler of .NET Common Language Runtime.

由于程序集是包含代码和元数据的自描述功能单元,因此无需在 C# 中使用 #include 指令和头文件。Because an assembly is a self-describing unit of functionality containing both code and metadata, there is no need for #include directives and header files in C#. 只需在编译程序时引用特定的程序集,即可在 C# 程序中使用此程序集中包含的公共类型和成员。The public types and members contained in a particular assembly are made available in a C# program simply by referencing that assembly when compiling the program. 例如,此程序使用 acme.dll 程序集中的 Acme.Collections.Stack 类:For example, this program uses the Acme.Collections.Stack class from the acme.dll assembly:

using System;
using Acme.Collections;
class Example
{
    static void Main() 
    {
        Stack s = new Stack();
        s.Push(1);
        s.Push(10);
        s.Push(100);
        Console.WriteLine(s.Pop());
        Console.WriteLine(s.Pop());
        Console.WriteLine(s.Pop());
    }
}

如果程序存储在文件 example.cs 中,编译 example.cs 时,可以使用编译器的 /r 选项引用 acme.dll 程序集:If the program is stored in the file example.cs, when example.cs is compiled, the acme.dll assembly can be referenced using the compiler’s /r option:

csc /r:acme.dll example.cs

这会创建 example.exe 可执行程序集,它将在运行时输出以下内容:This creates an executable assembly named example.exe, which, when run, produces the output:

100
10
1

使用 C#,可以将程序的源文本存储在多个源文件中。C# permits the source text of a program to be stored in several source files. 编译多文件 C# 程序时,可以将所有源文件一起处理,并且源文件可以随意相互引用。从概念上讲,就像是所有源文件在处理前被集中到一个大文件中一样。When a multi-file C# program is compiled, all of the source files are processed together, and the source files can freely reference each other—conceptually, it is as if all the source files were concatenated into one large file before being processed. 在 C# 中,永远都不需要使用前向声明,因为声明顺序无关紧要(除了极少数的例外情况)。Forward declarations are never needed in C# because, with very few exceptions, declaration order is insignificant. C# 并不限制源文件只能声明一种公共类型,也不要求源文件的文件名必须与其中声明的类型相匹配。C# does not limit a source file to declaring only one public type nor does it require the name of the source file to match a type declared in the source file.