Initializing Fields

The common language runtime (CLR) ensures that fields (as well as other kinds of variables) get zero-initialized when their memory is allocated. In practice, this means that reference typed fields will have a default value of null. For value types, all the object’s fields will recursively get zero-initialized. Primitive value types receive their default values; for example, an int will be set to 0. Consider the following example:

image

Creation of a new Basis instance will result in both direction fields to be set to null. For each new Vector instance, we’ll receive zero-initialized Points in both _start and _end. Such a zero-initialized ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.