Calling Custom Constructors with Initialization Syntax
The previous examples initialized Point types by implicitly calling the default constructor on the
type:
// Here, the default constructor is called implicitly.
Point finalPoint = new Point { X = 30, Y = 30 };
If you wish to be very clear about this, it is permissible to explicitly call the default constructor
as follows:
// Here, the default constructor is called explicitly.
Point finalPoint = new Point() { X = 30, Y = 30 };
Do be aware that when you are constructing a type using the new initialization syntax, you are
able to invoke any constructor defined by the class or structure. Our Point type currently defines a
two-argument constructor to set the (
x, y) position. Therefore, the following Point declaration
results in an X value of 100 and a Y value of 100, regardless of the fact that our constructor arguments
specified the values 10 and 16:
C H A P T E R 1 3
■
C # 2 0 0 8 L A N G U A G E F E AT U R E S
437
8849CH13.qxd 10/2/07 12:42 PM Page 437
Do'stlaringiz bilan baham: |