Prev | Current Page 227 | Next

Jon Skeet

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

The worst thing about writing code
using generics is that if you ever have to go back to C# 1, you??™ll miss them terribly.
In this chapter I haven??™t tried to cover absolutely every detail of what is and isn??™t
allowed when using generics??”that??™s the job of the language specification, and it
makes for very dry reading. Instead, I??™ve aimed for a practical approach, providing the
information you??™ll need in everyday use, with a smattering of theory for the sake of
academic interest.
We??™ve seen three main benefits to generics: compile-time type safety, performance,
and code expressiveness. Being able to get the IDE and compiler to validate your code
early is certainly a good thing, but it??™s arguable that more is to be gained from tools providing
intelligent options based on the types involved than the actual ???safety??? aspect.
Performance is improved most radically when it comes to value types, which no
longer need to be boxed and unboxed when they??™re used in strongly typed generic
APIs, particularly the generic collection types provided in .NET 2.0. Performance with
reference types is usually improved but only slightly.
Your code is able to express its intention more clearly using generics??”instead of a
comment or a long variable name required to describe exactly what types are
involved, the details of the type itself can do the work.


Pages:
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239