473,378 Members | 1,434 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,378 software developers and data experts.

More about iterator

Hello!

I have noticed that I can use iterator in a method and return an IEnumerable
like this
public IEnumerable<TReverse()
{
for (int i = data.Count -1 ; i >=0 ; i--)
yield return data[i];
}

I can test this Reverse by having this code
foreach (string word in bc.Reverse())
Console.WriteLine(word);

Now to my question If I instead have defined the Reverse method like this
public IEnumerator<TReverse()
{
for (int i = data.Count -1 ; i >=0 ; i--)
yield return data[i];
}
having IEnumerator as the return type I get compile
error saying "foreach statement cannot operate on variables of type
'System.Collections.Generic.IEnumerator<string>' because
'System.Collections.Generic.IEnumerator<string>' does not contain a public
definition for 'GetEnumerator'"

So I wonder how is the connection between a returning type for IEnumerable
and IEnumerator for a method using iterator and the statement call that is
used after the in keword in a foreach loop construction.

//Tony
Mar 7 '08 #1
0 953

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

Similar topics

13
by: Bulba! | last post by:
Hello Mr Everyone, From: http://docs.python.org/tut/node11.html#SECTION0011900000000000000000 "Define a __iter__() method which returns an object with a next() method. If the class defines...
38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
3
by: CoolPint | last post by:
If I were to design my own container and its iterators, I understand that I need to provide both "iterator" and "const_iterator" so that begin() and end() return appropriate ones depending on the...
0
by: CoolPint | last post by:
I am trying to write a generic heapsort (of course as a self-exercise) with Iterator interface: something like blow.... But I got into trouble finding out the Iterator to the Child node. If...
4
by: matthurne | last post by:
I am working through exercise 8-2 in Accelerated C++...I am implementing the function equal(b, e, b2) where b is an iterator for the first element in a container, e is an iterator pointing to one...
10
by: Chung Leong | last post by:
Just saw a message in pl.comp.lang.php, which states that the following message has appeared in the PHP CVS snapshot: Usage of {} to access string offsets is deprecated and will be removed in...
12
by: Mark P | last post by:
A couple template related questions: 1. In the code pasted below, why does the basic version get selected in the call to foo and the const version get selected in the call to bar? 2. When...
16
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
27
by: Steven D'Aprano | last post by:
I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. But I'm having problems writing a class that acts as an...
4
by: mkborregaard | last post by:
Hi, I have the weirdest problem, and I can not see what is going wrong. I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.