Click here to Skip to main content
15,914,594 members
Home / Discussions / C#
   

C#

 
GeneralRe: Serialization Problem Pin
William Winner24-May-10 13:10
William Winner24-May-10 13:10 
GeneralRe: Serialization Problem Pin
Kevin Marois24-May-10 13:13
professionalKevin Marois24-May-10 13:13 
GeneralRe: Serialization Problem Pin
Ravi Bhavnani24-May-10 14:35
professionalRavi Bhavnani24-May-10 14:35 
QuestionFtpWebRequest accepting a port number when UsePassive = true. Does this violate RFC 959? Pin
Alaric_24-May-10 8:47
professionalAlaric_24-May-10 8:47 
QuestionIterator, foreach, IEnumerable IEnumerator for a beginner :s Pin
bon_chan24-May-10 7:47
bon_chan24-May-10 7:47 
AnswerRe: Iterator, foreach, IEnumerable IEnumerator for a beginner :s Pin
Luc Pattyn24-May-10 8:04
sitebuilderLuc Pattyn24-May-10 8:04 
AnswerRe: Iterator, foreach, IEnumerable IEnumerator for a beginner :s Pin
Abhinav S24-May-10 18:23
Abhinav S24-May-10 18:23 
GeneralRe: Iterator, foreach, IEnumerable IEnumerator for a beginner :s Pin
bon_chan25-May-10 4:48
bon_chan25-May-10 4:48 
OK Thanks for your answers.

I understand a bit better, but yet I am still missing some parts.

Let's say we have a class called MyClass

So if we want to use foreach instruction, My Class must implement IEnumerable.
And then because IEnumerable contains GetEnumerator() signature, we have to implement that method in MyClass.
And because GetEnumerator() returns an IEnumerator, we need to implement IEnumerator interface as well (Reset(), MoveNext(), Current()). Is that correct?

But why does my Wrox book say:
Here is an extract of my wrox book:
<<
- To iterate over a class, use a method called GetEnumerator() with a return type of IEnumerator.
- To iterate over a class member, such as a method, use IEnumerable.
>>

I am confused Confused | :confused:

And why does the following code works without implementing IEnumerable???

<br />
using System;<br />
using System.Collections;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
<br />
namespace Ch11Ex03<br />
{<br />
    public class Primes<br />
    {<br />
        private long min;<br />
        private long max;<br />
<br />
        public Primes(): this(2, 100)<br />
        {<br />
        }<br />
<br />
        public Primes(long minimum, long maximum)<br />
        {<br />
            if (min < 2)<br />
            {<br />
                min = 2;<br />
            }<br />
            else<br />
            {<br />
                min = minimum;<br />
            }<br />
            max = maximum;<br />
        }<br />
<br />
        public IEnumerator GetEnumerator()<br />
        {<br />
            for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)<br />
            {<br />
                     bool isPrime = true;<br />
                     for (long possibleFactor = 2; possibleFactor <=<br />
(long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++)<br />
                {<br />
                    long remainderAfterDivision = possiblePrime % possibleFactor;<br />
                    if (remainderAfterDivision == 0)<br />
                    {<br />
                              isPrime = false;<br />
                              break;<br />
                    }<br />
                }<br />
                if (isPrime)<br />
                {<br />
                              yield return possiblePrime;<br />
                }<br />
            }<br />
        }<br />
    }<br />
}


<br />
class Program<br />
    {<br />
        static void Main(string[] args)<br />
        {<br />
            Primes primesFrom2To1000 = new Primes(2, 1000);<br />
            foreach (long i in primesFrom2To1000)<br />
            {<br />
                Console.Write("{0} ", i);<br />
            }<br />
            Console.ReadKey();<br />
        }<br />
    }<br />

Question2D Map collison help required Pin
venomation24-May-10 7:00
venomation24-May-10 7:00 
AnswerRe: 2D Map collison help required [modified] Pin
Alaric_24-May-10 8:55
professionalAlaric_24-May-10 8:55 
AnswerRe: 2D Map collison help required Pin
Alaric_24-May-10 9:07
professionalAlaric_24-May-10 9:07 
GeneralRe: 2D Map collison help required [modified] Pin
Alaric_25-May-10 4:48
professionalAlaric_25-May-10 4:48 
AnswerRe: 2D Map collison help required Pin
venomation26-May-10 5:59
venomation26-May-10 5:59 
QuestionMessage Removed Pin
24-May-10 3:30
Xpnctoc24-May-10 3:30 
AnswerRe: Form controls data binding sequence Pin
Luc Pattyn24-May-10 5:21
sitebuilderLuc Pattyn24-May-10 5:21 
GeneralRe: Form controls data binding sequence Pin
Xpnctoc24-May-10 5:30
Xpnctoc24-May-10 5:30 
GeneralRe: Form controls data binding sequence Pin
Luc Pattyn24-May-10 7:50
sitebuilderLuc Pattyn24-May-10 7:50 
GeneralRe: Form controls data binding sequence Pin
Xpnctoc24-May-10 8:00
Xpnctoc24-May-10 8:00 
GeneralRe: Form controls data binding sequence Pin
Luc Pattyn24-May-10 8:09
sitebuilderLuc Pattyn24-May-10 8:09 
GeneralRe: Form controls data binding sequence Pin
Xpnctoc24-May-10 8:15
Xpnctoc24-May-10 8:15 
QuestionVisual Studions 2005 and crystal reports 11.5 R2 Pin
krown24-May-10 2:56
krown24-May-10 2:56 
QuestionWhy am I recieving corrupt images? Pin
TimSWatson24-May-10 2:37
TimSWatson24-May-10 2:37 
AnswerRe: Why am I recieving corrupt images? Pin
Luc Pattyn24-May-10 4:53
sitebuilderLuc Pattyn24-May-10 4:53 
JokeRe: Why am I recieving corrupt images? Pin
Wes Aday24-May-10 8:27
professionalWes Aday24-May-10 8:27 
JokeRe: Why am I recieving corrupt images? Pin
DaveyM6924-May-10 9:06
professionalDaveyM6924-May-10 9:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.