 |
| 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
|
|
|
|

September 1st, 2012, 03:09 AM
|
|
Authorized User
|
|
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
|
|
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?
|
|

September 1st, 2012, 03:59 AM
|
|
Wrox Author
|
|
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
|
|
Quote:
Originally Posted by amigo1
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
// 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.
|
|
The Following User Says Thank You to ChristianNagel For This Useful Post:
|
|
|

September 1st, 2012, 08:52 AM
|
|
Authorized User
|
|
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
|
|
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
|
|

September 1st, 2012, 09:08 AM
|
|
Authorized User
|
|
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
|
|
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 .
|
|

September 1st, 2012, 09:31 AM
|
|
Wrox Author
|
|
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
|
|
You're welcome :-)
Quote:
Originally Posted by amigo1
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>.
|
|
The Following User Says Thank You to ChristianNagel For This Useful Post:
|
|
|

September 1st, 2012, 09:47 AM
|
|
Authorized User
|
|
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
|
|
thank you very much
|
|
 |