构造函数(C# 编程指南)Constructors (C# Programming Guide)

每当创建结构时,将会调用其构造函数。Whenever a class or struct is created, its constructor is called. 类或结构可能具有采用不同参数的多个构造函数。A class or struct may have multiple constructors that take different arguments. 使用构造函数,程序员能够设置默认值、限制实例化,并编写灵活易读的代码。Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read. 有关详细信息和示例,请参阅使用构造函数实例构造函数For more information and examples, see Using Constructors and Instance Constructors.

无参数构造函数Parameterless constructors

如果没有为类提供构造函数,默认情况下,C# 将创建一个会实例化对象并将成员变量设置为默认值的构造函数,如默认值表中所列。If you don't provide a constructor for your class, C# creates one by default that instantiates the object and sets member variables to the default values as listed in the Default Values Table. 如果没有为结构提供构造函数,C# 将依赖于隐式无参数构造函数,自动将值类型的每个字段初始化为其默认值,如默认值表中所列 。If you don't provide a constructor for your struct, C# relies on an implicit parameterless constructor to automatically initialize each field of a value type to its default value as listed in the Default Values Table. 有关详细信息和示例,请参阅实例构造函数For more information and examples, see Instance Constructors.

构造函数语法Constructor syntax

构造函数是一种方法,其名称与其类型的名称相同。A constructor is a method whose name is the same as the name of its type. 其方法签名仅包含方法名称和其参数列表;它不包含返回类型。Its method signature includes only the method name and its parameter list; it does not include a return type. 以下示例演示一个名为 Person 的类的构造函数。The following example shows the constructor for a class named Person.

public class Person
{
   private string last;
   private string first;
   
   public Person(string lastName, string firstName)
   {
      last = lastName;
      first = firstName;
   }
   
   // Remaining implementation of Person class.
}

如果某个构造函数可以作为单个语句实现,则可以使用表达式主体定义If a constructor can be implemented as a single statement, you can use an expression body definition. 以下示例定义 Location 类,其构造函数具有一个名为“name” 的字符串参数。The following example defines a Location class whose constructor has a single string parameter named name. 表达式主体定义给 locationName 字段分配参数。The expression body definition assigns the argument to the locationName field.

public class Location
{
   private string locationName;
   
   public Location(string name) => Name = name;

   public string Name
   {
      get => locationName;
      set => locationName = value;
   } 
}

静态构造函数Static constructors

前面的示例具有所有已展示的实例构造函数,这些构造函数创建一个新对象。The previous examples have all shown instance constructors, which create a new object. 类或结构也可以具有静态构造函数,该静态构造函数初始化类型的静态成员。A class or struct can also have a static constructor, which initializes static members of the type. 静态构造函数是无参数构造函数。Static constructors are parameterless. 如果未提供静态构造函数来初始化静态字段,C# 编译器会将静态字段初始化为其默认值,如默认值表中所列。If you don't provide a static constructor to initialize static fields, the C# compiler initializes static fields to their default value as listed in the Default Values Table.

以下示例使用静态构造函数来初始化静态字段。The following example uses a static constructor to initialize a static field.

public class Adult : Person
{
   private static int minimumAge;
   
   public Adult(string lastName, string firstName) : base(lastName, firstName)
   { }

   static Adult()
   {
      minimumAge = 18;
   }

   // Remaining implementation of Adult class.
}

也可以通过表达式主体定义来定义静态构造函数,如以下示例所示。You can also define a static constructor with an expression body definition, as the following example shows.

public class Child : Person
{
   private static int maximumAge;
   
   public Child(string lastName, string firstName) : base(lastName, firstName)
   { }

   static Child() => maximumAge = 18;

   // Remaining implementation of Child class.
}

有关详细信息和示例,请参阅静态构造函数For more information and examples, see Static Constructors.

本节内容In This Section

使用构造函数Using Constructors

实例构造函数Instance Constructors

私有构造函数Private Constructors

静态构造函数Static Constructors

如何:编写复制构造函数How to: Write a Copy Constructor

请参阅See also