Prev | Current Page 183 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

However, value types that are the same size and have the same in-memory
footprint as far as the GC is concerned could share code. At the time of this writing,
that??™s been of sufficiently low priority that it hasn??™t been implemented and it may well
stay that way.
This level of detail is primarily of academic interest, but it does have a
slight performance impact in terms of more code being JIT compiled.
However, the performance benefits of generics can be huge, and again
that comes down to having the opportunity to JIT to different code for
different types. Consider a List, for instance. In .NET 1.1, adding
individual bytes to an ArrayList would have meant boxing each
one of them, and storing a reference to each boxed value. Using
List has no such impact??”List has a member of type T[] to
replace the object[] within ArrayList, and that array is of the appropriate type,
taking the appropriate space. So List has a straight byte[] within it used to
store the elements of the array. (In many ways this makes a List behave like
a MemoryStream.)
Figure 3.3 shows an ArrayList and a List, each with the same six values.
(The arrays themselves have more than six elements, to allow for growth. Both
High
performance??”
avoids boxing
89 Advanced generics
List and ArrayList have a buffer, and they create a larger buffer when they
need to.


Pages:
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195