Results 1 to 5 of 5

Thread: [RESOLVED] [2.0] Whats the difference,...IEnumerable,Lists,..

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    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
  •  



Click Here to Expand Forum to Full Width