Prev | Current Page 380 | Next

Guy Fouch?©, Trey Nash

"Accelerated VB 2008"

Collections
Imports System.Collections.Generic
Public Class EntryPoint
Shared Sub Main()
End Sub
Public Sub NonGeneric(ByVal stack As Stack)
For Each o As Object In stack
Dim number As Integer = CInt(Fix(o))
Console.WriteLine(number)
Next o
End Sub
CHAPTER 12 n GENERICS 239
Public Sub Generic(ByVal stack As Stack(Of Integer))
For Each number As Integer In stack
Console.WriteLine(number)
Next number
End Sub
End Class
You??™ll notice that the IL code generated by the NonGeneric method has more instructions
than the generic version. Most of this is attributed to the type of coercing and unboxing that
the NonGeneric method must do. Furthermore, the NonGeneric method could possibly throw
an InvalidCastException if it encounters an object that you cannot explicitly cast and unbox
into an integer at run time.
Clearly, generics offer the compiler greater latitude to help it do its job by not stripping
away type information at compile time. However, you could argue that the efficiency gain is so
high that the primary motivator for generics in the CLR is to avoid unnecessary boxing operations.
Either way, both gains are significant and worth utilizing to the fullest extent.
Generic Type Placeholder Naming Conventions
Although there are no hard-and-fast rules for naming generic parameter placeholders, it is
recommended that you at least provide a name that is somewhat descriptive for how the type
is going to be used.


Pages:
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
Connie Talbot holz fenster pozycjonowanie katalog stron dakolen