9 Replies - 1865 Views - Last Post: 16 October 2012 - 02:52 AM Rate Topic: -----

#1 rnty  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 145
  • Joined: 14-August 12

Implementing IEnumerator

Posted 03 October 2012 - 02:18 AM

c
lass ListBox
{
     public IEnumerator<string> GetEnumerator
     {
         foreach(string s in strings)
         {
              yeild return s;
         }
     }
}

foreach(string s in ListBox)
   Console.WriteLine("{0}",s);


These are a few lines of code from the book "Learning C# 2005".Please explain how the IEnumerator is actually called and implemented here.
Thank you.

This post has been edited by tlhIn`toq: 03 October 2012 - 01:06 PM
Reason for edit:: code tags added


Is This A Good Question/Topic? 0
  • +

Replies To: Implementing IEnumerator

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Implementing IEnumerator

Posted 03 October 2012 - 02:42 AM

What do you already know? Do you know what an Interface is? And yield return?
Was This Post Helpful? 0
  • +
  • -

#3 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Implementing IEnumerator

Posted 03 October 2012 - 02:49 AM

Didn't the book already explain what Interface is and what IEnumerator is and how it is implemented?
Was This Post Helpful? 1
  • +
  • -

#4 rnty  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 145
  • Joined: 14-August 12

Re: Implementing IEnumerator

Posted 03 October 2012 - 03:00 AM

C# by O'Reily,that's the book I am using.
yield is used for returning a value to the IEnumerator object.Interfaces are abstract classes. and have been incorporated in C# to implement multiple inheritance.

The class inherits IEnumerable as a string and then implements the IEnumerator on a string list.What I don't understand is,the IEnumerator is used after the GetEnumerator().What happens to be the logic in the user-defined implementation case.

Thank you for replying

This post has been edited by rnty: 03 October 2012 - 03:06 AM

Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Implementing IEnumerator

Posted 03 October 2012 - 06:03 AM

'yield' doesn't return a value to the IEnumerator object.
Interfaces are not abstract classes.
Interfaces were not put in C# to support multiple inheritance (if they wanted to support multiple inheritance, they would have just done it).
The class doesn't inherit IEnumerable as a string. It doesn't inherit anything.


I seriously doubt your book said any of the things you said. I suggest slowing down and reading it again.
Was This Post Helpful? 3
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4482
  • View blog
  • Posts: 7,809
  • Joined: 08-June 10

Re: Implementing IEnumerator

Posted 03 October 2012 - 06:20 AM

I'm convinced that you're confusing IEnumerator with IEnumerable. The Enumerables have Enumerators that allow constructs like the foreach statement to traverse them. yield return lets you make an iterator method, but that returns an IEnumerable, not an IEnumerator. Look up IEnumerator on the MSDN and you'll see an implementation example.
Was This Post Helpful? 2
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,234
  • Joined: 21-June 11

Re: Implementing IEnumerator

Posted 03 October 2012 - 06:35 AM

View PostCurtis Rutland, on 03 October 2012 - 03:20 PM, said:

yield return lets you make an iterator method, but that returns an IEnumerable, not an IEnumerator.


The return type of an iterator method can be either an IEnumerable<T> or IEnumerator<T> (or their respective non-generic versions). Both are equally valid.
Was This Post Helpful? 2
  • +
  • -

#8 rnty  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 145
  • Joined: 14-August 12

Re: Implementing IEnumerator

Posted 03 October 2012 - 09:59 PM

foreach(string s in ListBox)  
Console.WriteLine("{0}",s); 



Are these the lines where the IEnumerator is called?

@Momerath:I will be grateful if you correct me.

Thank you all
Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,234
  • Joined: 21-June 11

Re: Implementing IEnumerator

Posted 04 October 2012 - 04:02 AM

Yes, the foreach is what's using the IEnumerator. foreach(Type variable in something) { DoStuff(variable); } is basically a shortcut for the following while loop:

IEnumerator enumerator = something.GetEnumerator();
while( enumerator.MoveNext() ) {
    Type variable = enumerator.Current;
    DoStuff(variable);
}



(Except that when using the foreach version no variable named enumerator will be defined, of course.)

Looking at that expanded version should make it obvious where and how the enumerator is used by the loop.
Was This Post Helpful? 0
  • +
  • -

#10 rnty  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 145
  • Joined: 14-August 12

Re: Implementing IEnumerator

Posted 16 October 2012 - 02:52 AM

Thank you everyone..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1