- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Copy ListDictionary to Array instance at the specified index in C#
To copy ListDictionary to Array instance at the specified index, the code is as follows −
Example
using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main(){ ListDictionary dict = new ListDictionary(); dict.Add(1, "Harry"); dict.Add(2, "Mark"); dict.Add(3, "John"); dict.Add(4, "Jacob"); dict.Add(5, "Tim"); dict.Add(6, "Sam"); dict.Add(7, "Tom"); dict.Add(8, "Kevin"); Console.WriteLine("ListDictionary elements..."); foreach(DictionaryEntry d in dict){ Console.WriteLine(d.Key + " " + d.Value); } DictionaryEntry[] dictArr = new DictionaryEntry[dict.Count]; Console.WriteLine("
Copying to Array instance..."); dict.CopyTo(dictArr, 0); for (int i = 0; i < dictArr.Length; i++) { Console.WriteLine("Key = "+dictArr[i].Key + ", Value = " + dictArr[i].Value); } } }
Output
This will produce the following output −
ListDictionary elements... 1 Harry 2 Mark 3 John 4 Jacob 5 Tim 6 Sam 7 Tom 8 Kevin Copying to Array instance... Key = 1, Value = Harry Key = 2, Value = Mark Key = 3, Value = John Key = 4, Value = Jacob Key = 5, Value = Tim Key = 6, Value = Sam Key = 7, Value = Tom Key = 8, Value = Kevin
Example
Let us now see another example −
using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main() { ListDictionary dict = new ListDictionary(); dict.Add(1, "Harry"); dict.Add(2, "Mark"); dict.Add(3, "John"); dict.Add(4, "Jacob"); dict.Add(5, "Tim"); Console.WriteLine("ListDictionary elements..."); foreach(DictionaryEntry d in dict) { Console.WriteLine(d.Key + " " + d.Value); } DictionaryEntry[] dictArr = new DictionaryEntry[10]; Console.WriteLine("
Copying to Array instance..."); dict.CopyTo(dictArr, 5); for (int i = 0; i < dictArr.Length; i++) { Console.WriteLine("Key = "+dictArr[i].Key + ", Value = " + dictArr[i].Value); } } }
Output
This will produce the following output −
ListDictionary elements... 1 Harry 2 Mark 3 John 4 Jacob 5 Tim Copying to Array instance... Key = , Value = Key = , Value = Key = , Value = Key = , Value = Key = , Value = Key = 1, Value = Harry Key = 2, Value = Mark Key = 3, Value = John Key = 4, Value = Jacob Key = 5, Value = Tim
- Related Articles
- Copy OrderedDictionary elements to Array instance at the specified index in C#
- Copy StringDictionary to Array at the specified index in C#
- Copy StringCollection at the specified index of array in C#
- Copying the entire ArrayList to 1-D Array starting at the specified index in C#
- Insert at the specified index in StringCollection in C#
- Remove the entry at specified index from OrderedDictionary in C#
- Remove the entry with specified key from ListDictionary in C#
- Add the specified key and value into the ListDictionary in C#
- Remove element at specified index of Collection in C#
- Java Program to copy an array from the specified source array
- Remove the element at the specified index of the ArrayList in C#
- Get or Set at specified index in StringCollection in C#
- Insert an element into the ArrayList at the specified index in C#
- Get or set the element at specified index in Collection in C#
- Insert an element into Collection at specified index in C#

Advertisements