Can someone, for the life of me, explain why this code won't compile with CS0305 as the compiler error
Thanks very much,
-Trey
----------------------------
using System;
using System.Collections.Generic;
public class MyContainer<T> : IEnumerable<T>
{
public void Add( T item ) {
impl.Add( item );
}
public void Add<R>( MyContainer<R> otherContainer ) {
foreach( item R in otherContainer ) {
impl.Add( item );
}
}
public IEnumerator<T> GetEnumerator() {
foreach( item T in impl ) {
yield return item;
}
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
private List<T> impl;
}
public class EntryPoint
{
static void Main() {
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 );
foreach( long l in lContainer ) {
Console.WriteLine( l );
}
}
}

IEnumerable<T> problem
Manjunathan
Hi all,
I figured out the problem. Someone suggested that the compiler may just need a little help by fully qualifying things. I wish I thought of that sooner. There, were of course, other syntax issues in the code that I had not gotten to since I was blocked on this one. However, just for good measure, I have attached what the code should really look like so it does not make me look like I was smoking crack. ;-)
Thanks,
-Trey
--------------------------------------------
using System;
using System.Collections.Generic;
public class MyContainer<T> : IEnumerable<T>
{
public void Add( T item ) {
impl.Add( item );
}
public void Add<R>( MyContainer<R> otherContainer,
Converter<R, T> converter ) {
foreach( R item in otherContainer ) {
impl.Add( converter(item) );
}
}
public IEnumerator<T> GetEnumerator() {
foreach( T item in impl ) {
yield return item;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return GetEnumerator();
}
private List<T> impl = new List<T>();
}
public class EntryPoint
{
static void Main() {
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;
}
}