Wrox Programmer Forums
|
C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 4.0 aka C# 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 8th, 2012, 01:30 AM
Authorized User
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default GetEnumerator () what is the use of the

what is the use of the GetEnumerator (). For example if I write a small code as

Code:
using System;  using System.Collections;
class MyClass : IEnumerator, IEnumerable {
int[] intst = { 1,2,3,4 };
int idx = -1;

public IEnumerator GetEnumerator() {
return this; //-------------------------------------------------------------->>>>>>>>>>>
}
// to implement IEnumerator.

public object Current {
get {
return intst[idx]; //--------------------------------------------------->>>>>>>>>>>>>>>
}
}
// Advance to the next object.
public bool MoveNext() {
if(idx == intst.Length-1) {
Reset(); 
return false;
}
idx++;
return true;
}

public void Reset() { idx = -1; }
}
class Enumer {
static void Main() {
MyClass mc = new MyClass();
// To display the contents of mc.
foreach(int i1 in mc)
Console.Write(i1 + " ");
Console.WriteLine();

}
}
Here is the output:
1 2 3 4

Here see the lines with multiple arrowheads. T he second one I can able to follow that it returns the current object
at idx ------- What i do not follow is the FIRST arrow headed code line

"
public IEnumerator GetEnumerator() {
return this;
"


---------- what is the use of it i mean?
 
Old November 28th, 2012, 03:55 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Quote:
Originally Posted by amigo1 View Post
what is the use of the GetEnumerator (). For example if I write a small code as

Code:
using System;  using System.Collections;
class MyClass : IEnumerator, IEnumerable {
int[] intst = { 1,2,3,4 };
int idx = -1;

public IEnumerator GetEnumerator() {
return this; //-------------------------------------------------------------->>>>>>>>>>>
}
// to implement IEnumerator.

public object Current {
get {
return intst[idx]; //--------------------------------------------------->>>>>>>>>>>>>>>
}
}
// Advance to the next object.
public bool MoveNext() {
if(idx == intst.Length-1) {
Reset(); 
return false;
}
idx++;
return true;
}

public void Reset() { idx = -1; }
}
class Enumer {
static void Main() {
MyClass mc = new MyClass();
// To display the contents of mc.
foreach(int i1 in mc)
Console.Write(i1 + " ");
Console.WriteLine();

}
}
Here is the output:
1 2 3 4

Here see the lines with multiple arrowheads. T he second one I can able to follow that it returns the current object
at idx ------- What i do not follow is the FIRST arrow headed code line

"
public IEnumerator GetEnumerator() {
return this;
"


---------- what is the use of it i mean?
The GetEnumerator method is defined by the interface IEnumerable. This method needs to return an object that implements the IEnumerator interface. In this case the class MyClass implements both IEnumerable and IEnumerator, that's why the GetEnumerator method can return the instance that implements IEnumerator, which is "this".
The caller of this method (the foreach statement) can access the members of the interface IEnumerator, such as MoveNext and Current which are implemented by MyClass.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
The Following User Says Thank You to ChristianNagel For This Useful Post:
amigo1 (November 30th, 2012)
 
Old November 30th, 2012, 02:25 AM
Authorized User
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default

thank you teacher
regards
amigo1





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 4: ForEach Error b/c ENTBaseEOList does not contain GetEnumerator RWoodmansee BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 2 September 26th, 2012 11:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.