TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Copy Dictionary

Copy all the elements in a Dictionary collection into another.
Copy Dictionary. The Dictionary has a copy constructor. When you pass an existing Dictionary to its constructor, it is copied. This is an effective way to copy a Dictionary's data.Constructor
Notes, original. When the original Dictionary is modified, the copy is not affected. Once copied, a Dictionary has separate memory locations to the original.Dictionary
First example. We can use a custom loop over the keys, but this is prone to code duplication and errors. It doesn't make any sense to write the loop yourself.

Part A: We copy the Dictionary into the second Dictionary "copy" by using the copy constructor.

Part B: The code adds a key to the first Dictionary. The key added after the copy was made is not in the copy.

Part C: We print the contents of both collections. The example demonstrates how the internal structures are copied.

Console
C# program that copies entire Dictionary using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, int> dictionary = new Dictionary<string, int>(); dictionary.Add("cat", 1); dictionary.Add("dog", 3); dictionary.Add("iguana", 5); // Part A: copy the Dictionary to a second object. Dictionary<string, int> copy = new Dictionary<string, int>(dictionary); // Part B: change the first Dictionary. dictionary.Add("fish", 4); // Part C: display the 2 Dictionaries. Console.WriteLine("--- Dictionary 1 ---"); foreach (var pair in dictionary) { Console.WriteLine(pair); } Console.WriteLine("--- Dictionary 2 ---"); foreach (var pair in copy) { Console.WriteLine(pair); } } } Output --- Dictionary 1 --- [cat, 1] [dog, 3] [iguana, 5] [fish, 4] --- Dictionary 2 --- [cat, 1] [dog, 3] [iguana, 5]
Internals. The Dictionary constructor that accepts IDictionary is implemented with this loop. My research established that the foreach-loop is fast and eliminates lookups.Foreach

Also: You could easily adapt the loop to copy to a Hashtable or List of KeyValuePair structs.

KeyValuePair
Implementation of Dictionary copy constructor: C# foreach (KeyValuePair<TKey, TValue> pair in dictionary) { this.Add(pair.Key, pair.Value); }
A summary. We copied an entire Dictionary to a new Dictionary. The Dictionary class does not define a Copy or CopyTo method. The copy constructor is the easiest way.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf