|
-
May 31st, 2007, 11:48 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2.0] Whats the difference,...IEnumerable,Lists,..
between,..
Doing this:
List<string> names = new List<string>();
And:
IEnumerable<string> names = new List<string>();
Thanks!
http://msdn2.microsoft.com/en-us/vcsharp/bb264519.aspx
-
May 31st, 2007, 06:55 PM
#2
Re: [2.0] Whats the difference,...IEnumerable,Lists,..
The difference is that the variables are different types. If you use the first code then you can access every member of the List class using the 'names' variable. If you use the second code then you can only access members of the IEnumerable interface using the 'names' variable, even though the object it refers to is a List object. You can see that using Intellisense. Using the second code you will get a severely reduced list of members from Intellisense if you place a dot after the variable name.
-
Jun 1st, 2007, 03:46 AM
#3
Thread Starter
Hyperactive Member
Re: [2.0] Whats the difference,...IEnumerable,Lists,..
Hi,Thanks jmcilhinney,
Just a newbie question,..
Just say I have a collection of "Person" objects.
So:
Code:
List<Person> PeopleList;
IEnumerable<Person> PeopleEnumerable;
private void LoadPeople()
{
PeopleList = new List<Person>();
PeopleEnumerable = new List<Person>();
for(int i = 0; i<1,000,000 ;i++)
{
Person p = new person();
p.name = i.toString();
PeopleList .add(p);
PeopleEnumerable.add(p);
}
So, stupid question;
Do both PeopleList and PeopleEnumerable contain 1,000,000 people items?
As PeopleEnumerable´s intellisense has less information than PeopleList,or as you said only the IEnumerable Interface members, does this mean from a performance point of view PeopleEnumerable, would be a better choice if memory is a issue?
Thanks
-
Jun 1st, 2007, 04:07 AM
#4
Re: [2.0] Whats the difference,...IEnumerable,Lists,..
It's very important to realise that variables and objects are quite different things. Think of a variable as a window through which you can see an object. The type of the variable controls what parts of the object you can see. If you create a List object but you look at it through an IEnumerable window then you can only see the IEnumerable parts of it. All the other parts are still there but you just can't see them. If you were to look at the same object through a List window then you'd be able to see all the other parts too.
Through an IEnumerable window you can look at any object that has IEnumerable parts and let you see those parts, i.e. an IEnumerable variable can refer to any type of object that implements the IEnumerable interface but it only provides access to the members of the IEnumerable interface. If you want to see the other parts you have use a window of another type.
-
Jun 1st, 2007, 04:21 PM
#5
Thread Starter
Hyperactive Member
Re: [2.0] Whats the difference,...IEnumerable,Lists,..
Great explanation,
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|