Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

Why can we use foreach without IEnumerator/IEnumerable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cody - 31 Aug 2007 12:37 GMT
Why was this design decision to use this kind of "duck typing" in that case?

All you have to do is have a method GetEnumerator() which returns an
object providing Current and MoveNext(). IEnumerator and IEnumerable are
not necessary. Why is that? And what are these interface good for then?
Just dummy/marker interfaces like Cloneable in java?
Marc Gravell - 31 Aug 2007 12:50 GMT
The interface is handy for when you need to talk about an abstract
"something that is enumerable, but I don't know what yet" - for
instance in the ctor of List<T>(IEnumerable<T>)

I can't explain the duck-typing ;-p

Marc
Jon Skeet [C# MVP] - 31 Aug 2007 13:46 GMT
> Why was this design decision to use this kind of "duck typing" in that case?

So that you could do strongly typed enumerations (including there
being no boxing penalty) before generics came along.

I suspect that if it was designed today with generics already present,
it wouldn't be done the same way. Having said that, I believe the
query expression transformations performed by the C# 3 compiler
require appropriate method names but not any particular interface.

> All you have to do is have a method GetEnumerator() which returns an
> object providing Current and MoveNext(). IEnumerator and IEnumerable are
> not necessary. Why is that? And what are these interface good for then?
> Just dummy/marker interfaces like Cloneable in java?

No, there are lots of things which *do* require IEnumerable. It's not
just for foreach!

Jon
cody - 31 Aug 2007 15:31 GMT
>> Why was this design decision to use this kind of "duck typing" in that case?
>
[quoted text clipped - 5 lines]
> query expression transformations performed by the C# 3 compiler
> require appropriate method names but not any particular interface.

Yes, I also heard of that.

>> All you have to do is have a method GetEnumerator() which returns an
>> object providing Current and MoveNext(). IEnumerator and IEnumerable are
[quoted text clipped - 5 lines]
>
> Jon

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.