
operation on the value type’s data. Generics come to the rescue and stop this madness. As an exam-
ple, compile the following code, and then load the assembly into ILDASM to compare the IL
generated for each of the methods that accept a Stack instance:
using System;
using System.Collections;
using System.Collections.Generic;
public class EntryPoint
{
static void Main() {
}
public void NonGeneric( Stack stack ) {
foreach( object o in stack ) {
int number = (int) o;
Console.WriteLine( number );
}
}
public void Generic( Stack<int> stack ) {
foreach( int number in stack ) {
Console.WriteLine( number );
}
}
}
You’ll notice that the IL code generated by the NonGeneric ...