Can someone shed any light on why this code will not compile? The compiler pitches a CS0305 error. This is very frustrating indeed. The problem seems to be with the IEnumerable.GetEnumerator() method and may be related to the controversial decision my the Whidbey folks to derive IEnumerable<T> from IEnumerable.
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 ) { foreach( item R in otherContainer ) { impl.Add( item ); } }
public IEnumerator<T> GetEnumerator() { foreach( item T in impl ) { yield return item; } }
public class EntryPoint { static void Main() { MyContainer<long> lContainer = new MyContainer<long>(); MyContainer<int> iContainer = new MyContainer<int>();
It is then complaining about some errors which look like valid syntax errors:
d:\Working\whidbey_cs_sandbox\Class1.cs(13,14): error CS0246: The type or namespace name 'item' could not be found (are you missing a using directive or an assembly reference?) d:\Working\whidbey_cs_sandbox\Class1.cs(13,19): error CS0412: 'R': a parameter or local variable cannot have the same name as a method type parameter d:\Working\whidbey_cs_sandbox\Class1.cs(15,16): error CS0103: The name 'item' does not exist in the current context d:\Working\whidbey_cs_sandbox\Class1.cs(21,14): error CS0246: The type or namespace name 'item' could not be found (are you missing a using directive or an assembly reference?) d:\Working\whidbey_cs_sandbox\Class1.cs(23,20): error CS0103: The name 'item' does not exist in the current context
->-----Original Message----- ->From: Unmoderated discussion of advanced .NET topics. ->[mailto:ADVANCED-DOTNET@DISCUSS.DEVELOP.COM] On Behalf Of Trey Nash ->Sent: Sunday, July 03, 2005 6:53 PM ->To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM ->Subject: [ADVANCED-DOTNET] Problem with Ienumerable<T> -> ->Can someone shed any light on why this code will not compile? -> The compiler pitches a CS0305 error. This is very ->frustrating indeed. The problem seems to be with the ->IEnumerable.GetEnumerator() method and may be related to the ->controversial decision my the Whidbey folks to derive ->IEnumerable<T> from IEnumerable. -> ->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 ) { -> 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 ); -> } -> } ->} -> ->=================================== ->This list is hosted by DevelopMentor. http://www.develop.com -> ->View archives and manage your subscription(s) at ->http://discuss.develop.com 07/03/2005 07:00:59 PM
=================================== This list is hosted by DevelopMentor http://www.develop.com
SWEET! That did the trick. Now, if only I had thought to fully qualify the beast.
I'm still on the fence about deriving IEnumerable<T> from IEnumerable and the same with IEnumerator. Sounds like someone needed a shortcut somewhere.
For good measure, I have included what the code should have looked like:
----------------------------------- 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; } }
public class EntryPoint { static void Main() { MyContainer<long> lContainer = new MyContainer<long>(); MyContainer<int> iContainer = new MyContainer<int>();
It is then complaining about some errors which look like valid syntax> errors:>
d:\Working\whidbey_cs_sandbox\Class1.cs(13,14): error CS0246: The type or> namespace name 'item' could not be found (are you missing a using> directive> or an assembly reference?)> d:\Working\whidbey_cs_sandbox\Class1.cs(13,19): error CS0412: 'R': a> parameter or local variable cannot have the same name as a method type> parameter> d:\Working\whidbey_cs_sandbox\Class1.cs(15,16): error CS0103: The name> 'item' does not exist in the current context> d:\Working\whidbey_cs_sandbox\Class1.cs(21,14): error CS0246: The type or> namespace name 'item' could not be found (are you missing a using> directive> or an assembly reference?)> d:\Working\whidbey_cs_sandbox\Class1.cs(23,20): error CS0103: The name> 'item' does not exist in the current context>
If you would like to report an abuse of our service, such as a spam message, please . Если Вы хотите пожаловаться на содержимое этой страницы, пожалуйста .