473,390 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

How to copy one list into other list

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.
Jun 27 '08 #1
5 52292
"Swapnil" <Sw*****@discussions.microsoft.comwrote in message
news:6E**********************************@microsof t.com...
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.
You can use the AddRange method, which accepts any IEnumerable type, such as
another list:

List<WhateveroriginalList = new List<Whatever>();
originalList.Add(...); //Load original list
....
List<WhatevercopiedList = 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.
Jun 27 '08 #2
On May 2, 9:06*pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
"Swapnil" <Swap...@discussions.microsoft.comwrote in message

news:6E**********************************@microsof t.com...
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.

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

List<WhateveroriginalList = new List<Whatever>();
originalList.Add(...); //Load original list
...
List<WhatevercopiedList = 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.
The List<Tconstructor is overloaded to take IEnumerable<T>. You can
try this:
Assuming firstList is of List<MyType>,
List<MyType>secondList = new List<MyType>(firstList);
Jun 27 '08 #3
On May 2, 10:12*pm, "Aneesh Pulukkul[MCSD.Net]" <anees...@gmail.com>
wrote:
On May 2, 9:06*pm, "Alberto Poblacion" <earthling-

quitaestoparacontes...@poblacion.orgwrote:
"Swapnil" <Swap...@discussions.microsoft.comwrote in message
news:6E**********************************@microsof t.com...
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.
You can use the AddRange method, which accepts any IEnumerable type, such as
another list:
List<WhateveroriginalList = new List<Whatever>();
originalList.Add(...); //Load original list
...
List<WhatevercopiedList = 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.

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

- Show quoted text -
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.
Jun 27 '08 #4

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

--
Regards,
Mudassar Hassan
Software Engineer
http://mudassarhassan.spaces.live.com/
"Swapnil" wrote:
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.
Jun 27 '08 #5
If you just want to add the elements of list A to list B:
B.AddRange(A);

"Mudassar Hassan" <mu*************@live.comwrote in message
news:89**********************************@microsof t.com...
>
List<Type>secondList = new List<Type>(firstList);

--
Regards,
Mudassar Hassan
Software Engineer
http://mudassarhassan.spaces.live.com/
"Swapnil" wrote:
>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.

Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Chinmoy Mukherjee | last post by:
when the node structure is like struct node { node *next; node *rand; (// random is supposed to point to another node int value; }
7
by: Steven Bethard | last post by:
Tom Anderson <twic@urchin.earth.li> wrote: > Sounds good. More generally, i'd be more than happy to get rid of list > comprehensions, letting people use list(genexp) instead. That would >...
7
by: Frank Millman | last post by:
Hi all Assume a 2-dimensional list called 'table' - conceptually think of it as rows and columns. Assume I want to create a temporary copy of a row called 'row', allowing me to modify the...
4
by: Sin Jeong-hun | last post by:
List<List<T>a=param; List<List<T>b=a; If I change b, then a is get changed. I want another copy of a, that is completely independent of a. I used double-nested for loop to copy each element...
3
by: timmy | last post by:
i make a copy of a list, and delete an item from it and it deletes it from the orginal as well, what the hell is going on?!?!?! #create the staff copy of the roster Roster2 = for ShiftLine in...
3
by: erik gartz | last post by:
Hello, I'm new to python and I'm having difficulty understanding the following code. Why doesn't the variable a contain , ] instead. Doesn't {} allocate new memory for the dictionary each time?...
3
by: theDuke | last post by:
Hello, i just wanted to ask what the best way would be to make a copy of a list of pointers to objects. The objects themselves each contain more pointers to other objects. All of these pointers...
3
by: Riccardo Murri | last post by:
Hello, I have some code that stops when trying to find a graph in a list of similar graphs:: (Pydb) list 110 try: 111 canonical = self.base 112 except ValueError: 113 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.