Constructors and Destructors

You can define all sorts of methods for a class, but there are two kinds of methods that have a very special place in an object’s lifetime: constructors and destructors.

Constructors

A constructor is a method that is called automatically when the program creates a new instance of a class. When part of the program creates a new instance of the class, the system calls the appropriate constructor before it returns the new object to the code that created it.

You can use a constructor to initialize the object’s properties and fields, get data for the object from a database, open resources, such as files, that the object will use, and generally prepare the object for use.

The syntax for constructors differs from language to ...

Get Start Here!™ Fundamentals of Microsoft® .NET Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.