• No results found

public Point topLeft, bottomRight; public Rectangle() {

// In C#, new on a value type just lets the constructor

// initialize the already allocated value type’s memory.

topLeft = new Point(1, 2);

bottomRight = new Point(100, 200);

}

}

A value type’s instance constructor is executed only when explicitly called. So if

Rectangle’s constructor didn’t initialize its topLeft and bottomRight fields using the

new operator to call Point’s constructor, the x and y fields in both Point fields would be 0.

In the Point value type defined earlier, no default parameterless constructor is defined. However, let’s rewrite that code as follows:

struct Point {

public Int32 x, y;

public Point() {

x = y = 5;

}

}

class Rectangle {

public Point topLeft, bottomRight;

public Rectangle() {

}

}

Now when a new Rectangle is constructed, what do you think the x and y fields in the two

Point fields, topLeft and bottomRight, would be initialized to: 0 or 5? (Hint: This is trick question.)

Many developers (especially those with a C++ background) would expect the C# compiler to emit code in Rectangle’s constructor that automatically calls Point’s default

parameterless constructor for the Rectangle’s two fields. However, to improve the run-time performance of the application, the C# compiler doesn’t automatically emit this code. In fact, many compilers will never emit code to call a value type’s default constructor automatically, even if the value type offers a parameterless constructor. To have a value type’s

parameterless constructor execute, the developer must add explicit code to call a value type’s constructor.

Based on the information in the preceding paragraph, you should expect the x and y fields in

Rectangle’s two Point fields to be initialized to 0 in the code shown earlier because there are no explicit calls to Point’s constructor anywhere in the code.

However, I did say that my original question was a trick question. The “trick” part is that C# doesn’t allow a value type to define a parameterless constructor. So the previous code won’t actually compile. The C# compiler produces the following error when attempting to compile that code: “error CS0568: Structs cannot contain explicit parameterless constructors.”

C# purposely disallows value types to define parameterless constructors to remove any confusion a developer might have about when that constructor gets called. If the constructor can’t be defined, the compiler can never generate code to call it automatically. Without a parameterless constructor, a value type’s fields are always initialized to 0/null.

Note

Strictly speaking, value type fields are guaranteed to be 0/null when the value type is a field nested within a reference type. However, stack-based value type fields are not guaranteed to be 0/null. For verifiability, any stack-based value type field must be written to prior to being read. If code could read a value type’s field prior to writing to the field, a security breach is possible. C# and other compilers that produce verifiable code ensure that all stack-based value types have their fields zeroed out or at least written to before being read so that a verification exception won’t be thrown at run time. For the most part, this means that you can assume that your value types have their fields initialized to 0 and you can completely ignore everything in this note.

Keep in mind that although C# doesn’t allow value types with parameterless constructors, the CLR does. So if the unobvious behavior described earlier doesn’t bother you, you can use another programming language (such as IL assembly language) to define your value type with a parameterless constructor.

Because C# doesn’t allow value types with parameterless constructors, compiling the following type produces the following: “error CS0573: ‘SomeType.x’: cannot have instance field initializers in structs.”

struct SomeValType {

Int32 x = 5;

}

In addition, because verifiable code requires that every field of a value type be written to prior to any field being read, any constructors that you do have for a value type must initialize all the type’s fields. The following type defines a constructor for the value type but fails to initialize all the fields:

struct SomeValType {

Int32 x, y;

// C# allows value types to have constructors that take parameters.

public SomeValType(Int32 x) {

this.x = x;

// Notice that y is not initialized here.

}

When compiling this type, the C# compiler produces the following: “error CS0171: Field ‘SomeValType.y’ must be fully assigned before control leaves the constructor.” To fix the problem, assign a value (usually 0) to y in the constructor.

Important

In C#, your source code defines a constructor method via a method whose name matches the name of the type itself. When the C# compiler compiles the source code, it detects the constructor method and adds an entry to the module’s method definition metadata table. In the table entry, the name of the constructor method is always .ctor. In Visual Basic source code, the developer defines a constructor by creating a method called New. This means that in Visual Basic, a developer can define a method (that is not a constructor method) whose name matches the name of the type. However, you should avoid this practice because the methods won’t be callable from other programming languages directly. You can call the methods using reflection, though, as discussed in Chapter 20.