Data Structures and Algorithms
with Object-Oriented Design Patterns in C# |
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.
Program: Complex constructors.
Program 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.