Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy Dictionary by value

Tags:

How can i copy Dictionary object by value in c#

like image 279
hippout Avatar asked Jan 16 '10 21:01

hippout


People also ask

Can you copy dictionaries in Python?

Use dict() The built-in dict() function can be used to make a shallow copy of a dictionary. This function will take the dictionary to be copied as an argument and return a shallow copy of that dictionary.

How do I copy one dictionary to another?

The dict. copy() method returns a shallow copy of the dictionary. The dictionary can also be copied using the = operator, which points to the same object as the original. So if any change is made in the copied dictionary will also reflect in the original dictionary.

How do you shallow copy a dictionary in Python?

The simplest way to create a copy of a Python dictionary is to use the . copy() method. This method returns a shallow copy of the dictionary.

How do I copy a key value pair to another dictionary in Python?

Using = operator to Copy a Dictionary in Python And directly copy it to a new object dict2 by the code line dict2=dict1. This operation copies references of each object present in dict1 to the new dictionary, dict2. Hence, updating any element of the dict2 will result in a change in dict1 and vice versa.


1 Answers

Create a new Dictionary passing the source dictionary in the constructor (of course, this will not copy the objects in the dictionary if they are reference types):

var copied = new Dictionary<KeyType, ValueType>(originalDictionary); 
like image 71
mmx Avatar answered Sep 30 '22 19:09

mmx