Constructors

The primary purpose of a constructor is to initialize an object. Initialization consists of setting the initial state of class fields. Because of definite assignment rules, it's often desirable to initialize your fields in constructors. This section shows how to gain full control over field initialization.

There are two categories of constructors: static and instance. Each category defines what fields can be initialized and the sequence of initialization. If need be, both types of constructors can be used in a single class. Just be careful to design your class properly. Using both types of constructors in the same class could be an indication of a class possibly supporting multiple roles—which may complicate a design.

Instance Constructors ...

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