C#构造函数在基类和父类中的执行顺序

1. Introduction

In object-oriented programming, the concept of inheritance allows us to create classes that inherit properties and methods from a base or parent class. When using inheritance in C#, one important aspect to consider is the order in which constructors are executed in both the base and derived classes. This article aims to provide a detailed explanation of the execution order of constructors in C# for both base and derived classes.

2. Constructor Execution Order

2.1 Base Class Constructor

When creating an instance of a derived class, the constructor in the base class is executed first. This ensures that any initialization or setup required in the base class is performed before any derived class-specific initialization.

It is important to note that if the base class contains multiple constructors, the derived class constructor must explicitly call the appropriate base class constructor using the `base` keyword. This ensures that the correct base class constructor is invoked.

For example, consider the following code snippet:

public class BaseClass

{

public BaseClass()

{

// Base class constructor without parameters

}

public BaseClass(int value)

{

// Base class constructor with parameter

}

}

public class DerivedClass : BaseClass

{

public DerivedClass() : base()

{

// Derived class constructor without parameters

}

public DerivedClass(int value) : base(value)

{

// Derived class constructor with parameter

}

}

In this example, the derived class `DerivedClass` explicitly calls the corresponding base class constructor using the `base()` syntax. This ensures that the base class constructor is executed before the derived class constructor.

2.2 Derived Class Constructor

After the base class constructor is executed, the constructor in the derived class is executed. This allows for any additional initialization or customization specific to the derived class. If the derived class has multiple constructors, the appropriate constructor is called based on the arguments provided during the instance creation.

Let's consider an example with a derived class that has multiple constructors:

public class DerivedClass : BaseClass

{

public DerivedClass() : base()

{

// Derived class constructor without parameters

}

public DerivedClass(int value) : base(value)

{

// Derived class constructor with parameter

}

public DerivedClass(string text) : base()

{

// Derived class constructor with different parameter type

}

}

In this example, the derived class `DerivedClass` has three constructors, each with different parameters. Depending on the arguments provided during the instance creation, the appropriate derived class constructor will be called, which in turn invokes the corresponding base class constructor before executing the derived class-specific code.

3. Constructor Chaining

Constructor chaining refers to the process of invoking one constructor from another constructor in the same class or in a base class. In C#, constructors can be chained using the `this` and `base` keywords.

3.1 Chaining to Base Class Constructor

To chain a constructor to a base class constructor, the `base` keyword is used followed by the appropriate arguments. This ensures that the base class constructor is called, allowing for code reuse and initialization of the base class members.

Consider the following code example:

public class BaseClass

{

public BaseClass()

{

// Base class constructor without parameters

}

public BaseClass(int value)

{

// Base class constructor with parameter

}

}

public class DerivedClass : BaseClass

{

public DerivedClass() : base()

{

// Derived class constructor without parameters

}

public DerivedClass(int value) : base(value)

{

// Derived class constructor with parameter

}

public DerivedClass(string text) : this()

{

// Derived class constructor chaining to another constructor in the derived class

}

}

In this example, the derived class `DerivedClass` has a constructor that chains to another constructor in the derived class using the `this()` syntax. This allows for reusing the common initialization logic in the derived class constructor while also invoking the base class constructor implicitly.

3.2 Chaining to Another Constructor in the Same Class

In addition to chaining to base class constructors, constructors can also be chained to other constructors within the same class using the `this` keyword. This enables constructors with different parameters to call each other, reducing code duplication.

Consider the following example:

public class MyClass

{

private int myValue;

public MyClass() : this(0)

{

// Default constructor chaining to another constructor with a default value

}

public MyClass(int value)

{

myValue = value;

// Parameterized constructor initializing myValue

}

}

In this example, the `MyClass` class has two constructors. The default constructor chains to the parameterized constructor using the `this()` syntax and provides a default value of 0 for the `myValue` field. This way, both constructors share the same initialization logic, reducing code repetition.

4. Conclusion

In C#, the execution order of constructors in both base and derived classes is crucial to ensure proper initialization and set up. The base class constructor is always executed before the derived class constructor. By understanding constructor execution order and constructor chaining, you can effectively design and implement your classes, ensuring that all necessary initialization steps are performed correctly.

后端开发标签