Data Structures and Algorithms with Object-Oriented Design Patterns in C#
next up previous contents index

The No-Arg Constructor

The constructor which takes no arguments is called the no-arg constructor  . For example, the no-arg constructor is invoked when a variable is declared like this:

Complex c = new Complex();
If there are no constructors defined in a C# class, the C# compiler provides a default no-arg constructor   . The default no-arg constructor does nothing. The fields simply the retain their initial, default values.

   program56760
Program: Complex constructors.

Program gif gives an implementation for the no-arg constructor. of the Complex class (lines 12-13). This constructor uses the an initializer called this. In C# one constructor can invoke another constructor by calling using the this initializer. In this case, the no-arg constructor invokes the two-arg constructor to set both real and imag fields to zero.


next up previous contents index

Bruno Copyright © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.