Performance Worries

Use of System.Object as the escape valve to make things as general as possible doesn’t only have bad characteristics with regard to compile-time safety; we have to give in on performance, as well. Not only does the runtime type checking required by cast instructions induce additional cost, keeping a value type object as a System.Object causes boxing. Recall how boxing involves wrapping a value in a heap-allocated box, therefore adding pressure to the garbage collector. And when the value needs to be retrieved with its original type, the symmetric unboxing operation occurs. Figure 15.2 illustrates boxing and unboxing.

Image

FIGURE ...

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.