Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

48

Part II

HANDLING DATA

 

 

 

Constructors

A constructor is a default method that is called when an object is initialized from a class. Each class has a constructor with a name that is the same as that of the class. A constructor does not return a value.However, unlike methods, you do not call a constructor. It is automatically called when you create an object of a class.

It is not necessary to declare a constructor for a class. If you do not explicitly

declare a constructor, C# automatically creates a default constructor for a class with the same name as that of the class.The following is an example of declaring a constructor:

<modifier> <constructor name>

 

Y

{

 

L

 

------------

F

 

 

}

 

M

 

Constructor modifiers include

 

public, private, protected, and internal. Inside

the block of the constructorAare the statements that are used to initialize the class

that contains the constructor.

 

 

 

E

 

 

T

 

 

To initialize the data members of a class, you can declare a constructor for the class and pass parameters to it.The parameters in this case are the initial values of the data members. To understand the concept of passing parameters to constructors, look at the following example:

public class Employee

{

public Employee()

{

string Name = ‘John’;

}

public Employee (string EmployeeName)

{

Name = EmployeeName;

}

}

In the previous code, the constructor with the name Employee() is declared in the class Employee. The constructor takes EmployeeName as the parameter and initializes the value of Name with EmployeeName. Therefore, when you create an instance of the Employee class,the object gets initialized.This prevents any other class from accessing the objects of the Employee class before initializing the object.

Team-Fly®

COMPONENTS OF C#

Chapter 3

49

 

 

 

After declaring a constructor, you need to use it to instantiate an object of the class. You use the new keyword to instantiate the object of the class.

Employee employee1 = new Employee (‘Smith’);

When you pass a value as a parameter, the new value overwrites the initial value. Therefore, the name of the employee is changed to Smith. However, if you do not want to change the default value, you can instantiate the constructor without passing a parameter to it, as shown in the following example:

Employee employee1 = new Employee();

As already discussed, if you do not explicitly declare a constructor, C# creates a default constructor. In this case, C# calls the default constructor of the direct base class.The base keyword is used to call the default base class constructor from the derived class. You can also use the this keyword if you want to call any other constructor of the base class.

Therefore, if you derive a class Salary from the class Employee, you can call the constructor of the Employee class from the Salary class.

class Employee

{

public Employee ()

{

--------------

}

}

class Salary : Employee

{

public Salary : base ()

{

--------------

}

}

As you know, it is essential to initialize an instance of a class whenever the instance is created. Similarly, it is also essential to destroy the instance of the class when it goes out of scope. To do this, C# contains destructors that are called when an instance of a class is destroyed.

50

Part II

HANDLING DATA

 

 

 

Destructors

Destructors are not extensively used in C#. To destroy resources from memory, C# uses the garbage collection mechanism. You have learned about this mechanism in Chapter 1, “Overview of the .NET Framework,” in the section “Garbage Collector.”

Similar to naming a constructor, the destructor takes the same name as that of the class. You can declare a destructor by using the destructor declaration statement. These statements include a tilde (~) sign followed by the name of the class.

public class Employee

{

~ Employee ()

{

-----------------

}

}

In the previous code, Employee() is the destructor of the Employee class. You can include the statements required to uninitialize variables of the class in the body of the destructor.

You cannot explicitly call a destructor. It is automatically invoked when an instance of a class is not used by any program. In addition, if destructors of the base and derived classes are to be invoked, the derived class destructor is invoked first, followed by the base class constructor.

In addition to the garbage collection system, C# provides you with the Finalize(), Dispose(), and Close() methods.These methods are used to clean up the memory after a resource is destroyed.

Finalize() Method

To destroy data members when they are not needed, you can create the Finalize() method. This method is automatically called when the class instance is deleted. The Finalize() method cleans the memory before the garbage collection system dereferences the object. You cannot call the Finalize() method because it gets automatically invoked when you call the destructor of the class.

Similar to a destructor declaration statement, you can declare the Finalize() method by using the tilde (~) sign followed by the name of the containing class.