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

About interface Enumerable and IEnumerator

Hello!

Interface IEnumerable consist of method GetEnumerator() which will return an
object of type IEnumerator.

Now to my question will every object that GetEnumerator return implement
Current, MoveNext and Reset.
I mean in this case I don't have to implement these three methods because
they are already accessible in the object that is returned from
GetEnumerator.

Is this correct understood ?

//Tony
Feb 28 '08 #1
3 1418
I mean in this case I don't have to implement these three methods because
they are already accessible in the object that is returned from
GetEnumerator.
If you are writing your own iterator, then yes - they must be implemented
(either explicit or implicit implementation is fine).

However, in C# 2 and above, a lot of this is done for you via "yield
return"; this makes creating iterators very painless.

Marc
Feb 28 '08 #2
On Feb 28, 2:25 pm, "Tony" <johansson.anders...@telia.comwrote:
Interface IEnumerable consist of method GetEnumerator() which will return an
object of type IEnumerator.

Now to my question will every object that GetEnumerator return implement
Current, MoveNext and Reset.
Yes, otherwise that wouldn't be implementing IEnumerator.
I mean in this case I don't have to implement these three methods because
they are already accessible in the object that is returned from
GetEnumerator.

Is this correct understood ?
It's hard to understand exactly what you mean. If you're *calling*
GetEnumerator() then you're using someone else's implementation to
start with - you wouldn't be implementing them in the first place.

What problem are you trying to solve?

Jon
Feb 28 '08 #3
Hello!

In this example I don't have to implement the three method that
GetEnumerator returns. Do you mean that this is the most common situation
that you don't have to implement these three methods?

I didn't fully understood what you meant in the previous answer.

Person[] array =
{
new Person("532513-1234", "Olle", 34),
new Person("123456-4321", "Pelle", 12),
new Person("987654-5678", "Stina", 44)
};

public Form1()
{
IEnumerator iter = array.GetEnumerator();

while (iter.MoveNext())
Console.WriteLine(iter.Current);
...
}

//Tony

"Marc Gravell" <ma**********@gmail.comskrev i meddelandet
news:O3**************@TK2MSFTNGP05.phx.gbl...
I mean in this case I don't have to implement these three methods
because
they are already accessible in the object that is returned from
GetEnumerator.

If you are writing your own iterator, then yes - they must be implemented
(either explicit or implicit implementation is fine).

However, in C# 2 and above, a lot of this is done for you via "yield
return"; this makes creating iterators very painless.

Marc


Feb 28 '08 #4

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

Similar topics

9
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
4
by: Michael K. Walter | last post by:
I'd like to create a strongly-typed collection of objects that are indexed by a string value (key is a string). I'd also like to allow users to iterate over the collection using a For-each loop...
4
by: Art | last post by:
Hi, I'm still very new to vb.net. I created a class, TableList, that inherits collections.base. I'd like to do a "for each" in another class that instantiates a TableList. I think I need to...
6
by: Ricky W. Hunt | last post by:
It's dawning on my a lot of my problems with VB.NET is I'm still approaching it in the same way I've programmed since the late 70's. I've always been very structured, flow-charted everything, used...
3
by: Senthil | last post by:
Hi , I am learning C# and now am stuck with a simple prorgam.Tried googling but didn't get an answer :(. The following program gives me three compilation errors.Can anyone enlighten me? Thanks. ...
0
by: paul | last post by:
Hi all, (Hopefully someone can answer this!) Is it true that in *all* interfaces, the body of the defined functions is missing, creating a level of abstraction for the functions, such as: ...
9
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? ...
6
by: =?Utf-8?B?RG9uYWxkIFhpZQ==?= | last post by:
In C# 2, this works just fine: public class foo : IEnumerable<string> { private string _list; public IEnumerator<stringGetEnumerator() { foreach (string s in _list) yield return s; }
4
by: jmDesktop | last post by:
In the code below from MSDN How do the PeopleEnum methods ever get called? foreach (Person p in peopleList) Console.WriteLine(p.firstName + " " + p.lastName); What is going on behind the...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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.