logo search
CSharp_Prog_Guide

Создание конструктора копии

В отличие от некоторых языков в C# отсутствует конструктор копии. Если создан новый объект и требуется скопировать значения из существующего объекта, необходимо создать соответствующий метод самостоятельно.

Пример

В данном примере Person класс содержит конструктор, который принимает другой объект типа Person в качестве аргумента. Содержимое полей в данном объекте назначается полям в новом объекте.

-----

Destructors

Destructors are used to destruct instances of classes.

Remarks

For example, the following is a declaration of a destructor for the class Car:

class Car

{

~ Car() // destructor

{

// cleanup statements...

}

}

The destructor implicitly calls Finalize on the base class of the object. Therefore, the previous destructor code is implicitly translated to the following code:

protected override void Finalize()

{

try

{

// Cleanup statements...

}

finally

{

base.Finalize();

}

}

This means that the Finalize method is called recursively for all instances in the inheritance chain, from the most-derived to the least-derived.