473,395 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Why this simple generic code does not compile?

public class TestGenericEnumerator<T:
System.Collections.Generic.IEnumerable<T>
{
public System.Collections.Generic.IEnumerator<TGetEnumera tor()
{
return null;
}
}
I don't understand why this is returning:

Error 6 'TestGenericEnumerator<T>' does not implement interface member
'System.Collections.Generic.IEnumerable<T>.GetEnum erator()'.
'TestGenericEnumerator<T>.GetEnumerator()' is either static, not public,
or has the wrong return type.

Anyone? :(
Jul 26 '07 #1
5 1449
Because IEnumerable<T: IEnumerable, and you haven't implemented the
non-generic form; just add:

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator() {
return GetEnumerator();
}

Jul 26 '07 #2
public System.Collections.Generic.IEnumerator<TGetEnumera tor()

Try this

public System.Collections.Generic.IEnumerator<TGetEnumera tor<T>()

Jul 26 '07 #3
Try this
public System.Collections.Generic.IEnumerator<TGetEnumera tor<T>()
The T in this method is now a different T to the class's template T
(which the compiler issues a CSO693 warning about) - but this means
that the above doesn't implement the interface, since you could use:

foreach(int i in new
TestGenericEnumerator<string>().GetEnumerator<int> ()) {...}

which doesn't do the job of implementing IEnumerable<string>. Good
idea, though.

Marc

Jul 26 '07 #4
Marc Gravell escribió:
Because IEnumerable<T: IEnumerable, and you haven't implemented the
non-generic form; just add:

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator() {
return GetEnumerator();
}

Thanks! Now it works, but, now I don't understand why I need this cast:

public class TestGenericEnumerator<T:
System.Collections.Generic.IEnumerable<T>
{
System.Collections.Generic.IEnumerator<T>
System.Collections.Generic.IEnumerable<T>.GetEnume rator()
{
return null;
}

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return (

//why is this cast needed!?
(IEnumerable<T>)
//end cast

this).GetEnumerator();
}
}

Thanks in advance.
Jul 26 '07 #5
why is this cast needed!?

Because you switched the generic version to an explicit
implementation, and so there is no this.GetEnumerator()

If you want to do this, consider having a single private
implementation that both the generic and non-generic versions invoke:

private IEnumerator<TGetEnumerator() {
return null; // the actual implementation
}
IEnumerator<TIEnumerable<T>.GetEnumerator() {
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}

Jul 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Roman Suzi | last post by:
Hi, I wonder, does Python support generic programming paradigm, and to what extent (I guess, it doesn't do it in full)? And (or) does it move in that direction? Will we ever see concept...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
2
by: Rune Vistnes | last post by:
Hey, I am trying to wrap an unmanaged library in managed c++ so that I can use this library in other .NET languages, such as C#. I've been successful for the most part this far, but I'm having a...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
1
by: sloan | last post by:
I have 1. An interface (generic). I simplified it to have one method. 2. A class which implements this interface. This class is also a Singleton. 3. A factory, (which right now only returns...
10
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
2
by: Andrus | last post by:
Winforms UI assembly has static FormManager.FormCreator method which creates forms taking entity as parameter. I need to pass this method to business objects in business assembly so that business...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.