Instance Constructors

You have already briefly been introduced to constructors in Chapter 5, “Your First Object-Oriented C# Program,” and since then you have seen simple implementations of them in several source code examples. The following section elaborates on this earlier introduction and looks at why constructors are needed and how they are used.

Why Do We Need Instance Constructors?

When an object is created, the underlying computer memory representing its instance variables are allocated as part of this process. But just after this allocation procedure (and before any automatic or manual initializations have taken place), the memory and, hence, the instance variables only holds arbitrary garbage left over from last time this memory was ...

Get C# Primer Plus 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.