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 recursively get zero-initialized. Primitive value types receive their default values; for example, an int will be set to 0. Consider the following example:
class Basis{ private Vector _direction1; private Vector _direction2;}class Vector{ private Point _start; private Point _end;}struct Point{ private int _x; private int _y;}
Creation of a new Basis instance results in both direction fields to be set to null. For each new Vector instance, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access