472,961 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 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 1896
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Doug Dew | last post by:
This won't compile: using IEnumerable<T> = System.Collections.Generic.IEnumerable<T>; namespace MyNamespace { public class MyClass<T> : IEnumerable<T> { // Appropriate stuff here }
10
by: jcc | last post by:
Hi guys, I'm a newbie to C#. My Visual Studio 2005 failed to compile the following code with error as 'HelloWorld.A' does not implement interface member...
15
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class...
5
by: Shikari Shambu | last post by:
Hi, I am trying to implement a collection that implements IEnumerable<T>. I keep getting the following error 'IEnumerator<...>.Current' in explicit interface declaration is not a member of...
1
by: =?Utf-8?B?RnJhbmNvaXNWaWxqb2Vu?= | last post by:
Hi there, Does anybody know how to return the results of an IEnumerable<typeas an array of the same type i.e type in a web service call without first having to collect all elements in the...
2
by: Morgan Cheng | last post by:
In .Net 2.0, Generics are introduced. I found that IEnumerable<T> inherites from IEnumerable. This makes implementation of IEnumerable<Thas to have two GetEnumerator methods defined( one for...
2
by: Tony Johansson | last post by:
Hello! Below I have a working program. I have one generic class called Farm<T> with this header definition public class Farm<T: IEnumerable<Twhere T : Animal Now to my question I changed the...
0
by: Marc Gravell | last post by:
You could bypass the List<T>'s tendency to use ICollection<Tby simply not giving it an ICollection<T- for example, with the C# 3 extension method below you could use: List<Tlist = new...
1
by: shapper | last post by:
Hello, On my MVC projects I often create classes which contains properties which are lists of other classes. Should I start using IQueryable<Tor IEnumerable<Tinstead of List<T>? What are...
2
by: Berryl Hesh | last post by:
Converting in the other direction , IEnumerable<Interfaceto List<ImplInterface>, I can just create a new List<ImplInterface(data) where data is IEnumerable<Interface>. How can I convert the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.