Click to See Complete Forum and Search --> : foreach and IEnumerator
George2
April 30th, 2008, 04:57 AM
Hello everyone,
Two questions,
1.
How foreach utilizes the IEnumerator interface and IEnumerable interface? Any there any docuements? I am interested in it.
2.
Pros and cons compared with using foreach and using simple index variable to iterate? Like,
for (int i = 0; i < abc.Count; i++)
{
// access abc [i] here
}
thanks in advance,
George
Arjay
April 30th, 2008, 06:50 AM
Use Lutz's .Net Reflector to view the original source code. Search on google to find the link to this free tool.
George2
April 30th, 2008, 08:44 AM
Thanks Arjay,
I have found some code, share here.
http://www.csharpfriends.com/Spec/index.aspx?specID=15.8.4.htm
I read the document. There are two expansions for foreach, but I can not distinguish the differences, what are the differences?
--------------------
2 If the collection expression is of a type that implements the collection pattern (as defined above), the expansion of the foreach statement is:
3 Otherwise; the collection expression is of a type that implements System.IEnumerable, and the expansion of the foreach statement is:
--------------------
Use Lutz's .Net Reflector to view the original source code. Search on google to find the link to this free tool.
regards,
George
Arjay
April 30th, 2008, 09:25 AM
It explains it here:
1 A type C is said to be a collection type if it implements the System.IEnumerable interface or implements the collection pattern by meeting all of the following criteria:
2 C contains a public instance method with the signature GetEnumerator(), that returns a struct-type, class-type, or interface-type, which is called E in the following text.
3 E contains a public instance method with the signature MoveNext() and the return type bool.
4 E contains a public instance property named Current that permits reading the current value. 5 The type of this property is said to be the element type of the collection type.
Take the above information and compare it to how IEnumerable is defined and then you will know the differences.
Say, when you get a chance can you post some code of projects you are working on?
George2
April 30th, 2008, 09:47 AM
Thanks Arjay,
But I think in both cases, the collector class needs to implement IEnumerator interface, since method GetEnumerator is from this interface and it is used in both cases, right?
It explains it here:
Take the above information and compare it to how IEnumerable is defined and then you will know the differences.
Say, when you get a chance can you post some code of projects you are working on?
regards,
George
JonnyPoet
May 1st, 2008, 03:11 AM
I read the document. There are two expansions for foreach, but I can not distinguish the differences, what are the differences?
The difference is very trivial. You only need to follow the text and compare the patterns.
In the case where the collection pattern is implemented directly you will have a GetEnumerator() method as part of the collection pattern so you can use it.
If you havn't you have to implement it using IEnumerable and therefore the GetEnumerator() is implemented by the Interface System.IEnumerable
Thats all about the differences. But I'm back to the basics.: ;) DO EXAMPLES. Do lots of examples. If you are trying to study this language dont only use the ECMA specifications. They are great, but as they have to be very general they can confuse you. Practice is the point. Where to learn a language: Best coming to the contry where it is spoken. Why ? The daily practice. Whats the badest way to learn a language: Reading the dictionary only. I dont say. cannot be done - no but this way is full of stones in the way and heavy rocks to climb.
If you have done half a gigabyte of examples and in between reading books then I'm sure you will be able to do it.
I have seen you said: I first need to know this... bla, bla.. before I do the example. Wrong pattern. During learning Try and error is totally ok.
Its the way babys learn to go. They do their first step and fall down. stand up and do the next and so on. Thats try and error. If you want to spare time by not doing the examples and to first find out theoretically -- you will not do it.-- You get no praxis. Stop your behaviour to be a 'Theoretical' one.:wave:
JonnyPoet
May 1st, 2008, 03:15 AM
But I think in both cases, the collector class needs to implement IEnumerator interface, since method GetEnumerator is from this interface and it is used in both cases, right?
TRY IT, simple Try it, my dear. take a class and implement a method GetEnumerator without implementing the Interface and try if it works with foreach. that answers your question.
You are figuring around nothing else
George2
May 1st, 2008, 07:50 AM
Thanks for your advice, JonnyPoet!
I have made some exercise and answered this question by myself. The last question, how could I see the expand statements (e.g. how C# compiler expand foreach) in Visual Studio?
TRY IT, simple Try it, my dear. take a class and implement a method GetEnumerator without implementing the Interface and try if it works with foreach. that answers your question.
You are figuring around nothing else
regards,
George
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.