C# 语言和 .NET Framework 介绍Introduction to the C# Language and the .NET Framework

C# 是类型安全的面向对象的精妙语言,可帮助开发者生成在 .NET Framework 上运行的各种安全可靠的应用程序。C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework. C# 可用于创建 Windows 客户端应用程序、XML Web service、分布式组件、客户端服务器应用程序、数据库应用程序等。You can use C# to create Windows client applications, XML Web services, distributed components, client-server applications, database applications, and much, much more. Visual C# 提供高级代码编辑器、方便使用的用户界面设计器、集成调试器和其他许多工具,以便你可以更轻松地开发基于 C# 语言和 .NET Framework 的应用程序。Visual C# provides an advanced code editor, convenient user interface designers, integrated debugger, and many other tools to make it easier to develop applications based on the C# language and the .NET Framework.

备注

阅读 Visual C# 文档的前提是,你已了解基本的编程概念。The Visual C# documentation assumes that you have an understanding of basic programming concepts. 如果是完完全全的初学者,不妨探索 Visual C# 速成版(可从 Web 获取)。If you are a complete beginner, you might want to explore Visual C# Express, which is available on the Web. 还可以利用介绍 C# 的书籍和 Web 资源来学习实用的编程技巧。You can also take advantage of books and Web resources about C# to learn practical programming skills.

C# 语言C# Language

C# 语法高度重视表达,但学习起来也很简单轻松。C# syntax is highly expressive, yet it is also simple and easy to learn. 任何熟悉 C、C++ 或 Java 的人都可以立即认出 C# 的大括号语法。The curly-brace syntax of C# will be instantly recognizable to anyone familiar with C, C++ or Java. 通常情况下,了解上述任何一种语言的开发者可以在很短的时间内就开始使用 C# 高效工作。Developers who know any of these languages are typically able to begin to work productively in C# within a very short time. C# 语法简化了 C++ 的许多复杂操作,并提供强大功能,如可以为 null 的值类型、枚举、委托、lambda 表达式和直接内存访问,而 Java 并不提供这些功能。C# syntax simplifies many of the complexities of C++ and provides powerful features such as nullable value types, enumerations, delegates, lambda expressions and direct memory access, which are not found in Java. C# 不仅支持泛型方法和类型,提升了类型安全性和性能,还支持迭代器,以便集合类的实现者可以定义方便客户端代码使用的自定义迭代行为。C# supports generic methods and types, which provide increased type safety and performance, and iterators, which enable implementers of collection classes to define custom iteration behaviors that are simple to use by client code. 语言集成查询 (LINQ)Language-Integrated Query (LINQ) 表达式让强类型查询成为最高级的语言构造。expressions make the strongly-typed query a first-class language construct.

作为面向对象的语言,C# 支持封装、继承和多形性这些概念。As an object-oriented language, C# supports the concepts of encapsulation, inheritance, and polymorphism. 所有变量和方法(包括作为应用程序入口点的 Main 方法)都封装在类定义中。All variables and methods, including the Main method, the application's entry point, are encapsulated within class definitions. 虽然类可能会直接继承一个父类,但可以实现任意数量的接口。A class may inherit directly from one parent class, but it may implement any number of interfaces. 若要用方法重写父类中的虚方法,必须使用 override 关键字,以免发生意外重定义。Methods that override virtual methods in a parent class require the override keyword as a way to avoid accidental redefinition. 在 C# 中,结构就像是轻量级类,是可以实现接口但不支持继承的堆栈分配类型。In C#, a struct is like a lightweight class; it is a stack-allocated type that can implement interfaces but does not support inheritance.

除了这些面向对象的基本原则,使用 C# 还可以通过以下多个创新语言构造更加轻松地开发软件组件:In addition to these basic object-oriented principles, C# makes it easy to develop software components through several innovative language constructs, including the following:

  • 封装的方法签名(名为“委托”),可实现类型安全事件通知。Encapsulated method signatures called delegates, which enable type-safe event notifications.

  • 用作私有成员变量的访问器的属性。Properties, which serve as accessors for private member variables.

  • 在运行时提供有关类型的声明性元数据的特性。Attributes, which provide declarative metadata about types at run time.

  • 内联的 XML 文档注释。Inline XML documentation comments.

  • 语言集成查询 (LINQ)Language-Integrated Query (LINQ),提供跨各种数据源的内置查询功能。which provides built-in query capabilities across a variety of data sources.

如果需要与其他 Windows 软件(如 COM 对象或本机 Win32 DLL)进行交互,可以在 C# 中通过名为“互操作”的过程来实现。If you have to interact with other Windows software such as COM objects or native Win32 DLLs, you can do this in C# through a process called "Interop". 借助互操作,C# 程序可以执行本机 C++ 应用程序可以执行的几乎任何操作。Interop enables C# programs to do almost anything that a native C++ application can do. 在直接内存访问非常关键的情况下,C# 甚至支持指针和“不安全”代码的概念。C# even supports pointers and the concept of "unsafe" code for those cases in which direct memory access is absolutely critical.

C# 生成过程比 C 和 C++ 更简单,比 Java 更灵活。The C# build process is simple compared to C and C++ and more flexible than in Java. 没有单独的头文件,也不要求按特定顺序声明方法和类型。There are no separate header files, and no requirement that methods and types be declared in a particular order. C# 源文件可以定义任意数量的类、结构、接口和事件。A C# source file may define any number of classes, structs, interfaces, and events.

可参阅下面的其他 C# 资源:The following are additional C# resources:

.NET Framework 平台体系结构.NET Framework Platform Architecture

C# 程序在 .NET Framework 上运行,这是 Windows 不可或缺的一部分,包括名为“公共语言运行时 (CLR)”的虚执行系统和一组统一的类库。C# programs run on the .NET Framework, an integral component of Windows that includes a virtual execution system called the common language runtime (CLR) and a unified set of class libraries. CLR 是由 Microsoft 执行的公共语言基础结构 (CLI) 的商业实现,CLI 是作为执行和开发环境(语言和库在其中无缝协作)创建依据的国际标准。The CLR is the commercial implementation by Microsoft of the common language infrastructure (CLI), an international standard that is the basis for creating execution and development environments in which languages and libraries work together seamlessly.

用 C# 编写的源代码被编译成符合 CLI 规范的中间语言 (IL)Source code written in C# is compiled into an intermediate language (IL) that conforms to the CLI specification. IL 代码和资源(如位图和字符串)存储在磁盘上名为“程序集”的可执行文件(扩展名通常为 .exe 或 .dll)中。The IL code and resources, such as bitmaps and strings, are stored on disk in an executable file called an assembly, typically with an extension of .exe or .dll. 程序集包含一个介绍程序集的类型、版本、区域性和安全要求的清单。An assembly contains a manifest that provides information about the assembly's types, version, culture, and security requirements.

当 C# 程序执行时,程序集会加载到 CLR 中,可能根据清单中的信息执行各种操作。When the C# program is executed, the assembly is loaded into the CLR, which might take various actions based on the information in the manifest. 然后,如果满足安全要求,CLR 会直接执行实时 (JIT) 编译,将 IL 代码转换成本机指令。Then, if the security requirements are met, the CLR performs just in time (JIT) compilation to convert the IL code to native machine instructions. CLR 还提供其他与自动垃圾回收、异常处理和资源管理相关的服务。The CLR also provides other services related to automatic garbage collection, exception handling, and resource management. CLR 执行的代码有时称为“托管代码”(而不是“非托管代码”),被编译成面向特定系统的本机语言。Code that is executed by the CLR is sometimes referred to as "managed code," in contrast to "unmanaged code" which is compiled into native machine language that targets a specific system. 下图展示了 C# 源代码文件、.NET Framework 类库、程序集和 CLR 的编译时和运行时关系。The following diagram illustrates the compile-time and run-time relationships of C# source code files, the .NET Framework class libraries, assemblies, and the CLR.

从 C# 源代码到计算机执行

语言互操作性是 .NET Framework 的一项重要功能。Language interoperability is a key feature of the .NET Framework. 由于 C# 编译器生成的 IL 代码符合公共类型规范 (CTS),因此 C# 生成的 IL 代码可以与 .NET 版本 Visual Basic、Visual C++ 或其他任何符合 CTS 的超过 20 种语言生成的代码进行交互。Because the IL code produced by the C# compiler conforms to the Common Type Specification (CTS), IL code generated from C# can interact with code that was generated from the .NET versions of Visual Basic, Visual C++, or any of more than 20 other CTS-compliant languages. 一个程序集可能包含多个用不同 .NET 语言编写的模块,且类型可以相互引用,就像是用同一种语言编写的一样。A single assembly may contain multiple modules written in different .NET languages, and the types can reference each other just as if they were written in the same language.

除了运行时服务之外,.NET Framework 还包括一个由 4000 多个已整理到命名空间中的类构成的扩展库,这些类提供各种实用功能,包括文件输入输出、字符串控制、XML 分析和 Windows 窗体控件。In addition to the run time services, the .NET Framework also includes an extensive library of over 4000 classes organized into namespaces that provide a wide variety of useful functionality for everything from file input and output to string manipulation to XML parsing, to Windows Forms controls. 典型的 C# 应用程序广泛使用 .NET Framework 类库来处理常见的“管道”零碎工作。The typical C# application uses the .NET Framework class library extensively to handle common "plumbing" chores.

有关 .NET Framework 的详细信息,请参阅 Microsoft.NET Framework 概述For more information about the .NET Framework, see Overview of the Microsoft .NET Framework.

请参阅See also