
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Copying the Hashtable elements to an Array Instance in C#
To copy the Hashtable elements to an Array Instance, the code is as follows−
Example
using System; using System.Collections; public class Demo { public static void Main(){ Hashtable hash = new Hashtable(); hash.Add("1", "AB"); hash.Add("2", "CD"); hash.Add("3", "EF"); hash.Add("4", "GH"); hash.Add("5", "IJ"); Console.WriteLine("Hashtable Key and Value pairs..."); foreach(DictionaryEntry entry in hash){ Console.WriteLine("{0} and {1} ", entry.Key, entry.Value); } Console.WriteLine("Copied to Array Instance..."); DictionaryEntry[] dictArr = new DictionaryEntry[hash.Count]; hash.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 −
Hashtable Key and Value pairs... 1 and AB 2 and CD 3 and EF 4 and GH 5 and IJ Copied to Array Instance... Key = 1, Value = AB Key = 2, Value = CD Key = 3, Value = EF Key = 4, Value = GH Key = 5, Value = IJ
Example
Let us now see another example −
using System; using System.Collections; public class Demo { public static void Main(){ Hashtable hash = new Hashtable(5); hash.Add("1", "AB"); hash.Add("2", "CD"); Console.WriteLine("Hashtable Key and Value pairs..."); foreach(DictionaryEntry entry in hash){ Console.WriteLine("{0} and {1} ", entry.Key, entry.Value); } Console.WriteLine("Copied to Array Instance..."); DictionaryEntry[] dictArr = new DictionaryEntry[5]; hash.CopyTo(dictArr, 2); 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 −
Hashtable Key and Value pairs... 1 and AB 2 and CD Copied to Array Instance... Key = , Value = Key = , Value = Key = 1, Value = AB Key = 2, Value = CD Key = , Value =
- Related Articles
- Copying the HybridDictionary entries to an Array Instance in C#
- Copying the Collection elements to an array in C#
- Copying BitArray elements to an Array in C#
- Copying the SortedList elements to an Array Object in C#
- Copying the Queue elements to one-dimensional array in C#
- Copying the elements of ArrayList to a new array in C#
- How to Get Hashtable Elements as Sorted Array?
- Copy OrderedDictionary elements to Array instance at the specified index in C#
- How to Convert Hashtable into an Array?
- What are the different ways of copying an array into another array in Java?
- How to store/update Hashtable Elements?
- Remove all elements from the Hashtable in C#
- Can you assign an Array of 100 elements to an array of 10 elements in Java?
- Create new instance of an Array with Java Reflection Method
- Copy ListDictionary to Array instance at the specified index in C#

Advertisements