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 September 12th, 2012, 03:20 AM
Authorized User
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default iterator confusion

Code:
void Main()
{
 	myClass mc = new myClass();
	foreach (char ch in mc)
	Console.WriteLine(ch);
	 }


class myClass 
{ char[] chrs = new char[] {'A','B','C','D','E','F'} ;

//this iterator returns the characters in the char array
 
 public IEnumerator GetEnumerator()
 { foreach (char c in chrs)
 yield return c; // omitting this statement will Not return a value:- 'UserQuery.myClass.GetEnumerator() ': not all code
// paths return a value
//actually this statement creates the iterator block
 Console.WriteLine(chrs);
 // why does c in WriteLine : 'c' does not exist in the current context. puttting  chrs in WriteLine makes the query successful
 }
}
o/p

A
B
C
D
E
F
ABCDEF
 
Old September 13th, 2012, 11: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
Code:
void Main()
{
 	myClass mc = new myClass();
	foreach (char ch in mc)
	Console.WriteLine(ch);
	 }


class myClass 
{ char[] chrs = new char[] {'A','B','C','D','E','F'} ;

//this iterator returns the characters in the char array
 
 public IEnumerator GetEnumerator()
 { foreach (char c in chrs)
 yield return c; // omitting this statement will Not return a value:- 'UserQuery.myClass.GetEnumerator() ': not all code
// paths return a value
//actually this statement creates the iterator block
 Console.WriteLine(chrs);
 // why does c in WriteLine : 'c' does not exist in the current context. puttting  chrs in WriteLine makes the query successful
 }
}
The variable 'c' is declared within the foreach statement, and only valid within the foreach statement block. As you do not declare a block, you have only a single statement in the foreach: yield return. After this statement, the variable 'c' is not declared and thus not available in the context.
If you add curly brackets to the foreach, you can use the 'c' variable.
__________________
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 (September 14th, 2012)
 
Old September 14th, 2012, 09:27 AM
Authorized User
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default thank you

thank you teacher
How are you?
regards
amigo1





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 2 - Iterator for loop example question craig9001 BOOK: Expert PHP and MySQL 0 July 8th, 2012 06:57 AM
iterator Pattern harpua BOOK: Professional PHP Design Patterns 5 January 5th, 2011 11:40 PM
Iterator Scope Problem smokey_gun Javascript How-To 1 July 4th, 2007 01:26 AM
iterator..... s Larryz C# 2005 0 May 9th, 2007 04:06 PM





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