CHAPTER 11 ■ GENERICS 287
MyContainer<long> lContainer = new MyContainer<long>();
MyContainer<int> iContainer = new MyContainer<int>();
lContainer.Add( 1 );
lContainer.Add( 2 );
iContainer.Add( 3 );
iContainer.Add( 4 );
lContainer.Add( iContainer,
EntryPoint.IntToLongConverter );
foreach( long l in lContainer ) {
Console.WriteLine( l );
}
}
static long IntToLongConverter( int i ) {
return i;
}
}
static long IntToLongConverter( int i ) {
return i;
}
}
First of all, take note of the syntax of the generic Add<R> method, and also notice that there are
two overloads of Add in MyContainer<T>. Clearly, you need to have a method to add instances of type
T—thus, the need for Add( T ). However, it would be really handy to be able to add an entire range
of objects from another ...