471,337 Members | 1,178 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

IEnumerable<T>

I've given this interface some thought, the benefits to using it seem to be

01: I can use "var" in a foreach

//Works
List<SomeClasslist = new List<SomeClass>();
foreach(var item in list)
item.MethodOnSomeClass();

//Doesn't work
ArrayList list2 = new ArrayList();
foreach(var item in list2)
item.MethodOnSomeClass(); //item is treated as System.Object
02: Type checking

//This compiles
ArrayList list2 = new ArrayList();
list2.Add(new Person());

foreach(NotAPersonClass item in list2)
item.MethodOnNotAPersonClass();
//This does not compile
List<Personlist = new List<Person>();
foreach(NotAPersonClass item in list)
item.MethodOnNotAPersonClass();
Are there any other benefits of IEnumerable<Tover IEnumerable that I have
not considered?

Pete
Aug 25 '08 #1
3 1822
"Peter Morris" <mr*********@SPAMgmail.comwrote in message
news:O%****************@TK2MSFTNGP02.phx.gbl...
I've given this interface some thought, the benefits to using it seem to
be
>
01: I can use "var" in a foreach

//Works
List<SomeClasslist = new List<SomeClass>();
foreach(var item in list)
item.MethodOnSomeClass();

//Doesn't work
ArrayList list2 = new ArrayList();
foreach(var item in list2)
item.MethodOnSomeClass(); //item is treated as System.Object
02: Type checking

//This compiles
ArrayList list2 = new ArrayList();
list2.Add(new Person());

foreach(NotAPersonClass item in list2)
item.MethodOnNotAPersonClass();
//This does not compile
List<Personlist = new List<Person>();
foreach(NotAPersonClass item in list)
item.MethodOnNotAPersonClass();
Are there any other benefits of IEnumerable<Tover IEnumerable that I
have
not considered?

Here is a couple:-

When working with a value type it doesn't get boxed.

foreach on a IEnumerable would do an 'explicit' cast 'implicitly' at
run-time. The extra code needed is eliminated by tighter compile time info.


--
Anthony Jones - MVP ASP/ASP.NET
Aug 25 '08 #2
Peter Morris wrote:
Are there any other benefits of IEnumerable<Tover IEnumerable that
I have not considered?
Well of course the "big ticket item" benefit is that there are a bunch
of Linq extension methods for IEnumerable<Tand not there for
IEnumerable.

Cheers Tim.

--

Aug 25 '08 #3
Peter Morris <mr*********@SPAMgmail.comwrote:
I've given this interface some thought, the benefits to using it seem to be
<snip>
Are there any other benefits of IEnumerable<Tover IEnumerable that I have
not considered?
What kind of use are you talking about? If you're *implementing*
IEnumerable and considering whether or not to use the generic form,
then *absolutely* you should - it provides a much more expressive API.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 29 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by jcc | last post: by
15 posts views Thread by Gustaf | last post: by
5 posts views Thread by Shikari Shambu | last post: by
1 post views Thread by =?Utf-8?B?RnJhbmNvaXNWaWxqb2Vu?= | last post: by
2 posts views Thread by Morgan Cheng | last post: by
1 post views Thread by shapper | last post: by
2 posts views Thread by Berryl Hesh | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.