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 1st, 2012, 03:09 AM
Authorized User
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default please explain this --a wrox coding

using System;
using System.Collections;
namespace Wrox.ProCSharp.Arrays
{
public class HelloCollection{
public IEnumerator < string > GetEnumerator()
{
yield return "Hello";
yield return "World";
}
}

===============


public void HelloWorld()
{
var helloCollection = new HelloCollection();
foreach (var s in helloCollection)
{
Console.WriteLine(s);
}
}
}


====================


public class HelloCollection
{
public IEnumerator GetEnumerator()
{
return new Enumerator(0);

// What and does this statement does
// Also how does PROBABLY case value 0 goes to get to 1 and 2 and break:
// the break is mandatory , but is a fallthrough allowed

}
public class Enumerator: IEnumerator<string>, IEnumerator, IDisposable
{
private int state;
private string current;
public Enumerator(int state)
{
this.state = state;
}
bool System.Collections.IEnumerator.MoveNext()
{
switch (state)
{
case 0:
current = "Hello";
state = 1;
return true;
case 1:
current = "World";
state = 2;
return true;
case 2:
break;
}
return false;
}
void System.Collections.IEnumerator.Reset()
{
throw new NotSupportedException();

//Why so?
string System.Collections.Generic.IEnumerator<string>.Cur rent
{
get
{
return current;
}
}
object System.Collections.IEnumerator.Current
{
get
{
return current;
}
}
void IDisposable.Dispose()

//why So?{
}
}


Why are 2 classes names the same HelloCollection?
 
Old September 1st, 2012, 03:59 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
Why are 2 classes names the same HelloCollection?
The text between the code lines should answer this. The first two code blocks you've in your post are the ones you can write. The 2nd HelloCollection shows the code as it is created from the C# compiler out of the yield statements.

Regarding your inline questions in the code:
Quote:
Originally Posted by amigo1 View Post
// What and does this statement does
// Also how does PROBABLY case value 0 goes to get to 1 and 2 and break:
// the break is mandatory , but is a fallthrough allowed
With a switch statement you can either do a break, a goto (explicit fallthrough), or a return. The generated code does a return, and thus neither a break nor a fallthrough.

Hope this helps.
__________________
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 7th, 2012)
 
Old September 1st, 2012, 08:52 AM
Authorized User
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default

Hello Teacher
This is my first letter to you
I just cannot believe that it is you who answered my query.
Thank you very much teacher ---
- well very often than not I will disturb this forum from now on
Regards
amigo1
 
Old September 1st, 2012, 09:08 AM
Authorized User
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default

oops I just forgot this

public class Enumerator: IEnumerator<string>, IEnumerator, IDisposable

As IEnumerator<string> is derived from IEnumerator and IDisposable, is it mandatory to inherit them too here in this case .
 
Old September 1st, 2012, 09:31 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

You're welcome :-)

Quote:
Originally Posted by amigo1 View Post
oops I just forgot this

public class Enumerator: IEnumerator<string>, IEnumerator, IDisposable

As IEnumerator<string> is derived from IEnumerator and IDisposable, is it mandatory to inherit them too here in this case .
It's not necessary to derive from the other interfaces, as this is done implicitly by the IEnumerator<string> interface. It is necessary to implement all the members of IEnumerator and IDisposable in both cases, if you explicitly derive from these interfaces, or if you implicitly derive from these interfaces by deriving from IEnumerable<string>.
__________________
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 7th, 2012)
 
Old September 1st, 2012, 09:47 AM
Authorized User
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default

thank you very much





Similar Threads
Thread Thread Starter Forum Replies Last Post
Who can explain more for these ? sbwfnyiiiu BOOK: Beginning Visual C# 2010 0 November 2nd, 2011 04:37 AM
Convert DAO Coding to ADO Coding udaybg1 VB How-To 0 August 9th, 2011 08:50 AM
explain please rein Javascript 3 January 31st, 2005 09:47 AM
Could somebody explain this to me, please? czambran BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 3 June 7th, 2004 04:05 PM
Wrox and Wrox.com FAQs (updated March 2, 2004) jminatel General Announcements 0 June 6th, 2003 09:23 AM





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