473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 1916
"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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.