/*Quote from:
Book Accelerated C# 2005
* By Trey Nash
* ISBN: 1-59059-717-6
* 432 pp.
* Published: Aug 2006
* Price: $39.99
*/
using System;
using System.Collections.Generic;
public class MyContainer : IEnumerable
{
public void Add( T item ) {
impl.Add( item );
}
// Converter is a new delegate type introduced
// in the .NET Framework that can be wired up to a method that
// knows how to convert the TInput type into a TOutput type.
public void Add( MyContainer otherContainer,Converter converter ) {
foreach( R item in otherContainer ) {
impl.Add( converter(item) );
}
}
public IEnumerator GetEnumerator() {
foreach( T item in impl ) {
yield return item;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return GetEnumerator();
}
private List impl = new List();
}
public class EntryPoint
{
static void Main() {
MyContainer lContainer = new MyContainer();
MyContainer iContainer = new MyContainer();
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;
}
}
1
2
3
4