Showing posts with label copying large collections without iteration. Show all posts
Showing posts with label copying large collections without iteration. Show all posts

Sunday, October 30, 2011

Copying large collections without iteration

I had go through an  interesting  situation where I had to copy items from one collection to another.The problem was that the collection was very large and iterating through it was not a very good option . I solved the problem using the snippet below
This example is just a representation of the problem. In My situation the stack had 100 thousand integers  int .I had to copy it to a ordered list of integers

using System;
using System.Collections.Generic;
using System.Collections;

namespace testCollection
{
    class Program
    {
        static void Main(string[] args)
        {

            //Stack of integers
            Stack<int> stackCollection = new Stack<int>();
            stackCollection.Push(1);
            stackCollection.Push(2);
            stackCollection.Push(3);
            stackCollection.Push(4);
            stackCollection.Push(5);

            //copying the items of stack into the ordered list
            List<int> listCollection = new List<int>(stackCollection);
            Console.WriteLine(listCollection.Count);


   }

   }
}