DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

.NET Gotcha #1: List vs. Collection Constructor

Slobodan Pavkov user avatar by
Slobodan Pavkov
·
Sep. 23, 12 · Interview
Like (0)
Save
Tweet
Share
4.65K Views

Join the DZone community and get the full member experience.

Join For Free

don’t google it!

do you know (without googling it) what will this console application display when executed?

class program
{
    static void main(string[] args)
    {
        var originalnumbers = new list<int> { 1, 2, 3, 4, 5, 6 };

        var list = new list<int>(originalnumbers);
        var collection = new collection<int>(originalnumbers);

        originalnumbers.removeat(0);

        displayitems(list, "list items: ");
        displayitems(collection, "collection items: ");

        console.readline();
    }

    private static void displayitems(ienumerable<int> items, string title)
    {
        console.writeline(title);
        foreach (var item in items)
            console.write(item);
        console.writeline();
    }
}

if you try to think logically, you expect that both list and collection will contain numbers from 1 to 6 when they are displayed, right?

wrong, otherwise this would not be a proper gotcha ;)

here is the actual output of the application (surprise, surprise):

do you know why?

answer is actually very simple. if you check the msdn documentation for collection constructor that accepts generic ilist you will see this text:

initializes a new instance of the collection class as a wrapper for the specified list.

that means that this constructor creates just a wrapper around the existing ilist, so adding or removing items to the original list will also affect the new collection we just created,  because its just a wrapper and uses original list under the hood.

on the other hand list constructor that accepts ienumerable<t> behaves properly and does what you would logically expect it to do:

initializes a new instance of the list<t> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.

so when you create a new list and pass an existing list to its constructor it will create a proper new empty list, and then copy all the references from original list to the new list.

because of that, the new list is ‘detached’ from the original and this is why it remains unchanged even if we modify the original list.

moral of the story

this is simply how microsoft made these two classes (i would not say its very consistent or good but who am i to judge, right?) and as long as you know it – its fine.

and if you don’t know this – it can bite you really hard.

all that you can do is to try to remember this, and also: rtfm .

btw, did i said that this can be an excellent job interview question? ;)

until next dot net gotcha, keep reading those manuals…


Net (command)

Published at DZone with permission of Slobodan Pavkov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps Roadmap for 2022
  • How to Quickly Build an Audio Editor With UI
  • Exploring the Benefits of Cloud Computing: From IaaS, PaaS, SaaS to Google Cloud, AWS, and Microsoft
  • Explainer: Building High Performing Data Product Platform

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: