C# 语言介绍A Tour of the C# Language

C#(读作“See Sharp”)是一种简单易用的新式编程语言,不仅面向对象,还类型安全。C# (pronounced "See Sharp") is a simple, modern, object-oriented, and type-safe programming language. C# 源于 C 语言系列,C、C++、Java 和 JavaScript 程序员很快就可以上手使用。C# has its roots in the C family of languages and will be immediately familiar to C, C++, Java, and JavaScript programmers. 本教程概述了该语言的主要组件。This tour provides an overview of the major components of the language. 如果想要通过交互式示例探索语言,请尝试我们的 C# 简介教程。If you want to explore the language through interactive examples, try our introduction to C# tutorials.

C# 是一种面向对象的语言。不仅如此,C# 还进一步支持面向组件的编程。C# is an object-oriented language, but C# further includes support for component-oriented programming. 当代软件设计越来越依赖采用自描述的独立功能包形式的软件组件。Contemporary software design increasingly relies on software components in the form of self-contained and self-describing packages of functionality. 此类组件的关键特征包括:为编程模型提供属性、方法和事件;包含提供组件声明性信息的特性;包含自己的文档。Key to such components is that they present a programming model with properties, methods, and events; they have attributes that provide declarative information about the component; and they incorporate their own documentation. C# 提供了语言构造来直接支持这些概念,让 C# 成为一种非常自然的语言,可用于创建和使用软件组件。C# provides language constructs to support directly these concepts, making C# a very natural language in which to create and use software components.

多项 C# 功能有助于构造可靠耐用的应用程序:垃圾回收可自动回收无法访问的未使用对象占用的内存;异常处理提供了一种结构化的可扩展方法来执行错误检测和恢复;C# 语言的类型安全设计禁止读取未初始化的变量、为范围之外的数组编制索引或执行未检查的类型转换。Several C# features aid in the construction of robust and durable applications: Garbage collection automatically reclaims memory occupied by unreachable unused objects; exception handling provides a structured and extensible approach to error detection and recovery; and the type-safe design of the language makes it impossible to read from uninitialized variables, to index arrays beyond their bounds, or to perform unchecked type casts.

C# 采用统一的类型系统C# has a unified type system. 所有 C# 类型(包括 intdouble 等基元类型)均继承自一个根 object 类型。All C# types, including primitive types such as int and double, inherit from a single root object type. 因此,所有类型共用一组通用运算,任何类型的值都可以一致地进行存储、传输和处理。Thus, all types share a set of common operations, and values of any type can be stored, transported, and operated upon in a consistent manner. 此外,C# 还支持用户定义的引用类型和值类型,从而支持对象动态分配以及轻量级结构的内嵌式存储。Furthermore, C# supports both user-defined reference types and value types, allowing dynamic allocation of objects as well as in-line storage of lightweight structures.

为了确保 C# 程序和库能够持续兼容,C# 设计非常注重版本控制To ensure that C# programs and libraries can evolve over time in a compatible manner, much emphasis has been placed on versioning in C#'s design. 许多编程语言很少关注这个问题,因此,当引入新版依赖库时,用这些语言编写的程序会出现更多不必要的中断现象。Many programming languages pay little attention to this issue, and, as a result, programs written in those languages break more often than necessary when newer versions of dependent libraries are introduced. C# 设计中受版本控制加强直接影响的方面包括:单独的 virtualoverride 修饰符,关于方法重载决策的规则,以及对显式接口成员声明的支持。Aspects of C#'s design that were directly influenced by versioning considerations include the separate virtual and override modifiers, the rules for method overload resolution, and support for explicit interface member declarations.

Hello worldHello world

“Hello, World”程序历来都用于介绍编程语言。The "Hello, World" program is traditionally used to introduce a programming language. 下面展示了此程序的 C# 代码:Here it is in C#:

using System;

class Hello
    static void Main()
        Console.WriteLine("Hello, World");

C# 源文件的文件扩展名通常为 .csC# source files typically have the file extension .cs. 假设“Hello, World”程序存储在文件 hello.cs 中,则可以使用下列命令行编译此程序:Assuming that the "Hello, World" program is stored in the file hello.cs, the program might be compiled using the command line:

csc hello.cs

这会生成名为 hello.exe 的可执行程序集。which produces an executable assembly named hello.exe. 运行此应用程序生成以下输出:The output produced by this application when it is run is:

Hello, World


编译 csc 命令实现的是完整框架,可能并不所有平台都适用。The csc command compiles for the full framework, and may not be available on all platforms.

“Hello, World”程序始于引用 System 命名空间的 using 指令。The "Hello, World" program starts with a using directive that references the System namespace. 命名空间提供了一种用于组织 C# 程序和库的分层方法。Namespaces provide a hierarchical means of organizing C# programs and libraries. 命名空间包含类型和其他命名空间。例如,System 命名空间包含许多类型(如程序中引用的 Console 类)和其他许多命名空间(如 IOCollections)。Namespaces contain types and other namespaces—for example, the System namespace contains a number of types, such as the Console class referenced in the program, and a number of other namespaces, such as IO and Collections. 借助引用给定命名空间的 using 指令,可以非限定的方式使用作为相应命名空间成员的类型。A using directive that references a given namespace enables unqualified use of the types that are members of that namespace. 由于使用 using 指令,因此程序可以使用 Console.WriteLine 作为 System.Console.WriteLine 的简写。Because of the using directive, the program can use Console.WriteLine as shorthand for System.Console.WriteLine.

“Hello, World”程序声明的 Hello 类只有一个成员,即 Main 方法。The Hello class declared by the "Hello, World" program has a single member, the method named Main. Main 方法是使用静态修饰符进行声明。The Main method is declared with the static modifier. 实例方法可以使用关键字 this 引用特定的封闭对象实例,而静态方法则可以在不引用特定对象的情况下运行。While instance methods can reference a particular enclosing object instance using the keyword this, static methods operate without reference to a particular object. 按照约定,Main 静态方法是程序的入口点。By convention, a static method named Main serves as the entry point of a program.

程序的输出是由 System 命名空间中 Console 类的 WriteLine 方法生成。The output of the program is produced by the WriteLine method of the Console class in the System namespace. 此类由标准类库提供。默认情况下,编译器会自动引用标准类库。This class is provided by the standard class libraries, which, by default, are automatically referenced by the compiler.

关于 C#,要介绍的内容还有很多。There's a lot more to learn about C#. 下面各主题概述了 C# 语言元素。The following topics provide an overview of the elements of the C# language. 通过这些概述,可以了解 C# 语言所有元素的基本信息,并获得深入了解 C# 语言元素所需的信息:These overviews will provide basic information about all elements of the language and give you the information necessary to dive deeper into elements of the C# language:

  • 程序结构Program Structure
    • 了解 C# 语言中的关键组织概念:程序命名空间类型成员程序集Learn the key organizational concepts in the C# language: programs, namespaces, types, members, and assemblies.
  • 类型和变量Types and Variables
    • 了解 C# 语言中的值类型引用类型变量Learn about value types, reference types, and variables in the C# language.
  • 表达式Expressions
    • 表达式是在操作数运算符的基础之上构造而成。Expressions are constructed from operands and operators. 表达式生成的是值。Expressions produce a value.
  • 语句Statements
    • 语句用于表示程序的操作。You use statements to express the actions of a program.
  • 类和对象Classes and objects
    • 是最基本的 C# 类型。Classes are the most fundamental of C#'s types. 对象是类实例。Objects are instances of a class. 类是使用成员生成的,此主题也对此进行了介绍。Classes are built using members, which are also covered in this topic.
  • 结构Structs
    • 与类不同,结构是属于值类型的数据结构。Structs are data structures that, unlike classes, are value types.
  • 数组Arrays
    • 数组是一种数据结构,其中包含许多通过计算索引访问的变量。An array is a data structure that contains a number of variables that are accessed through computed indices.
  • 接口Interfaces
    • 接口定义了可由类和结构实现的协定。An interface defines a contract that can be implemented by classes and structs. 接口可以包含方法、属性、事件和索引器。An interface can contain methods, properties, events, and indexers. 接口不提供所定义的成员的实现代码,仅指定必须由实现接口的类或结构提供的成员。An interface does not provide implementations of the members it defines—it merely specifies the members that must be supplied by classes or structs that implement the interface.
  • 枚举Enums
    • 枚举类型是包含一组已命名常量的独特值类型。An enum type is a distinct value type with a set of named constants.
  • 委托Delegates
    • 委托类型表示对具有特定参数列表和返回类型的方法的引用。A delegate type represents references to methods with a particular parameter list and return type. 通过委托,可以将方法视为可分配给变量并可作为参数传递的实体。Delegates make it possible to treat methods as entities that can be assigned to variables and passed as parameters. 委托类似于其他一些语言中的函数指针概念,但与函数指针不同的是,委托不仅面向对象,还类型安全。Delegates are similar to the concept of function pointers found in some other languages, but unlike function pointers, delegates are object-oriented and type-safe.
  • 特性Attributes
    • 使用特性,程序可以指定关于类型、成员和其他实体的附加声明性信息。Attributes enable programs to specify additional declarative information about types, members, and other entities.