CSharp/C# >> How to copy one list into other list

by U3dhcG5pbA » Fri, 02 May 2008 23:27:01 GMT

hi,

I want to copy the elements of one list to the other list.
Is there any function to do this operation in C#.

Plz explain with example.

CSharp/C# >> How to copy one list into other list

by Alberto Poblacion » Sat, 03 May 2008 00:06:38 GMT



You can use the AddRange method, which accepts any IEnumerable type, such as
another list:

List<Whatever> originalList = new List<Whatever>();
originalList.Add(...); //Load original list
...
List<Whatever> copiedList = new List<Whatever>();
copiedList.AddRange(originalList);
//Now you have another list with a copy of all elements.
Note that if "Whatever" is a reference type, you will have copied the
references to the elements, not the contents of the elements.

CSharp/C# >> How to copy one list into other list

by Aneesh Pulukkul[MCSD.Net] » Sat, 03 May 2008 01:12:35 GMT

On May 2, 9:06爌m, "Alberto Poblacion" <earthling-




The List<T> constructor is overloaded to take IEnumerable<T>. You can
try this:
Assuming firstList is of List<MyType>,
List<MyType>secondList = new List<MyType>(firstList);

CSharp/C# >> How to copy one list into other list

by Aneesh Pulukkul[MCSD.Net] » Sat, 03 May 2008 01:36:00 GMT

On May 2, 10:12爌m, "Aneesh Pulukkul[MCSD.Net]" < XXXX@XXXXX.COM >





Forgot to reiterate Alberto's point- Even though the list instance is
different, the MyType objects in both list are the same i.e
references. So if you change any object in one list it'll be reflected
in the other list also.

CSharp/C# >> RE: How to copy one list into other list

by TXVkYXNzYXIgSGFzc2Fu » Mon, 09 Jun 2008 14:40:02 GMT


List<Type>secondList = new List<Type>(firstList);

--
Regards,
Mudassar Hassan
Software Engineer
http://mudassarhassan.spaces.live.com/

CSharp/C# >> How to copy one list into other list

by Fred Mellender » Mon, 09 Jun 2008 15:10:41 GMT

If you just want to add the elements of list A to list B:
B.AddRange(A);

Similar Threads

1. Copy Items from a Generic List into multiple Generic Lists - CSharp/C#

2. copy list to object : list

Hi All,

I have a simple console app listed in its entirety below. I was just 
wondering why I can't copy a list from point A to point B?

namespace PracticeCopyListToObject
{
    class Program
    {
        static void Main(string[] args)
        {
            ReceiptList list2 = new ReceiptList();

            List<Receipt> list = new List<Receipt>();
            list.Add(new Receipt { InvoiceDate = "10/29/2009", ReceiptId = 2 
});
            list.Add(new Receipt { InvoiceDate = "10/28/2009", ReceiptId = 1 
});

            list2 = list; //how would  i copy to my object
        }
    }

    public class Receipt
    {
        public int ReceiptId { get; set; }
        public string InvoiceDate { get; set; }
    }

    public class ReceiptList : List<Receipt>
    {
        public ReceiptList()
        {
            
        }
    }
}


thanks,
rodchar

3. How to copy only non-repeated lists elements to a single list using functors

4. How to copy only non-repeated lists elements to a single list using functors

5. How to copy only non-repeated lists elements to a single list using functors

6. How to list all drivers and its folder in one list box like win explorer

Hi, all,

I'm now writing an application to let use to select certain folder as folder 
to store files that, for example, download files. How can I list all drivers 
and it's folder in one list box like win explorer. e.g.
- Desktop
    - My Computer
        + -Floppy (A:)
        + -Xxx (C:)
        - -YYY(D:)
            + APP
            + BCB

I now use BCB6 to develop it.

Very thanks
Susan


7. Copy List(Of type) to another List(Of type) - VB.Net

8. populate one list box with selected values from another list box

I would really appreciate some help on this:  I am having some trouble
discovering how to create the classic dual list box control.  Where
there are four buttons (move selected, move all, move back selected,
move back all) in between to listboxes.  

Thanks, 
Steve C.