473,671 Members | 2,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 52301
"Swapnil" <Sw*****@discus sions.microsoft .comwrote in message
news:6E******** *************** ***********@mic rosoft.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<Whateveror iginalList = new List<Whatever>( );
originalList.Ad d(...); //Load original list
....
List<Whateverco piedList = new List<Whatever>( );
copiedList.AddR ange(originalLi st);
//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-
quitaestoparaco ntes...@poblaci on.orgwrote:
"Swapnil" <Swap...@discus sions.microsoft .comwrote in message

news:6E******** *************** ***********@mic rosoft.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<Whateveror iginalList = new List<Whatever>( );
originalList.Ad d(...); //Load original list
...
List<Whateverco piedList = new List<Whatever>( );
copiedList.AddR ange(originalLi st);
//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<Tconstruct or is overloaded to take IEnumerable<T>. You can
try this:
Assuming firstList is of List<MyType>,
List<MyType>sec ondList = new List<MyType>(fi rstList);
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-

quitaestoparaco ntes...@poblaci on.orgwrote:
"Swapnil" <Swap...@discus sions.microsoft .comwrote in message
news:6E******** *************** ***********@mic rosoft.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<Whateveror iginalList = new List<Whatever>( );
originalList.Ad d(...); //Load original list
...
List<Whateverco piedList = new List<Whatever>( );
copiedList.AddR ange(originalLi st);
//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<Tconstruct or is overloaded to take IEnumerable<T>. You can
try this:
Assuming firstList is of List<MyType>,
List<MyType>sec ondList = new List<MyType>(fi rstList);- 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>secon dList = new List<Type>(firs tList);

--
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******** *************** ***********@mic rosoft.com...
>
List<Type>secon dList = new List<Type>(firs tList);

--
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
4036
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
1609
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 > obviously be a Py3k thing, though. Alex Martelli wrote: > I fully agree, but the BDFL has already (tentatively, I hope) > Pronounced that the form will stay in Py3K as syntax sugar for > list(...). I find that to be a truly hateful prospect, but...
7
1920
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 contents of 'row' without modifying the contents of 'table'. I used to fall into the newbie trap of 'row = table', but I have
4
33125
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 manually. Is there any more efficent way to do that? Something like, List<List<T>b=CreateClone(a); Thanks.
3
1276
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 Roster: #delete phone number from staff copy Roster2.append(ShiftLine) del Roster2
3
1287
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? It almost appears as if the 2nd dictionary created overwrites the first one. Thanks for your help, Erik , ] a = {'x':i} , ]
3
2989
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 should point to new objects in the copied list and not to the original objects. I thought it might be good to write a copy constructor for the list but I'm not sure how I would do that. Do I have to use inheritance? Any help would be appreciated....
3
2411
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 raise ValueError, \
0
8472
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8909
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8596
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1801
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.