471,893 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,893 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 52131
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Chinmoy Mukherjee | last post: by
7 posts views Thread by Frank Millman | last post: by
4 posts views Thread by Sin Jeong-hun | last post: by
3 posts views Thread by timmy | last post: by
3 posts views Thread by erik gartz | last post: by

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.