Behind the Scenes of Constructors

To satisfy our technical curiosity, we would like to know how constructors are called when writing code as shown earlier. As you already know, constructors are special methods that cannot be called directly. Their special treatment reaches beyond the managed code language compilers, though. The runtime also needs to treat them specially because they are tightly coupled with memory-allocation mechanisms and such.

Let’s illustrate matters by using the ILDASM for the TimeSpan constructor call, as shown in Figure 6.13.

Image

FIGURE 6.13 A constructor call for a value type unraveled.

In fact, a few different cases for constructor ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

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