Re: foreach and IEnumerator
Use Lutz's .Net Reflector to view the original source code. Search on google to find the link to this free tool.
Re: foreach and IEnumerator
Thanks Arjay,
I have found some code, share here.
http://www.csharpfriends.com/Spec/in...cID=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:
--------------------
Quote:
Originally Posted by Arjay
Use Lutz's .Net Reflector to view the original source code. Search on google to find the link to this free tool.
regards,
George
Re: foreach and IEnumerator
It explains it here:
Quote:
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?
Re: foreach and IEnumerator
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?
Quote:
Originally Posted by Arjay
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
Re: foreach and IEnumerator
Quote:
Originally Posted by George2
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:
Re: foreach and IEnumerator
Quote:
Originally Posted by George2
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
Re: foreach and IEnumerator
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?
Quote:
Originally Posted by JonnyPoet
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