Programming Microsoft® Composite UI Application Block and Smart Client Software Factory
by David S. Platt
C. Simplest Example
The facing page shows an example of using a generic collection to obtain the benefits of strong typing and avoid the performance drag of boxing. I first instantiate an object of the class System.Collections.Generic.List, which is an expandable array, the generic equivalent of ArrayList. In its angle brackets I pass the type that I want it to hold, in this case, int. The developers of this class have written it in such a manner as to check the type that it is passed and allocate storage for holding its value, so boxing and unboxing don't take place, as you can see in Figure A-3. (We see these techniques in the next example.)
The compiler now knows that this list accepts only integers, so storing an integer in it is allowed, as ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access