
- 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
How to Convert Hashtable to String?
The hashtable collection in C# is a non-generic collection of elements. Each element of the hashtable is represented as a key-value pair. The keys of the hashtable are non-null and unique. The values can be duplicated and/or null.
The Hashtable class of C# Systems.The collections interface is the representation of a hashtable collection. This class provides various constructors, methods, and properties to manipulate the hashtable collection.
We can also convert hashtables into other collections like arrays, ArrayList, etc., and also to a string representation.
In this article, let’s discuss how we can convert a hashtable collection into a string.
How to Convert hashtable Items to a String?
Note that to convert hashtable items to a string, the hashtable class does not provide a direct method like Java. There is also no in-built property to do the conversion. Hence we need to devise our own method to convert hashtable elements to a string representation.
For this, we will have to traverse the hashtable collection and then copy the key and value of each item to the string. We can put a delimiter to separate each key-value item in the string.
To actually program this using C#, we use an IDictionaryEnumerator that will iterate through each item in the hashtable. The enumerator holds each item (Key & value) of the hashtable. We can then copy each key and value to the string using a loop as shown below.
IDictionaryEnumerator enumerator = phonetics.GetEnumerator(); while (enumerator.MoveNext()) { text += enumerator.Key + ", "; text += enumerator.Value + "
"; }
As we can see in the above piece of code, after each key-value pair is copied to the string, we insert a delimiter ‘
’ so that each item is separated.
Example
The complete program in C# to convert hashtable items to a string is shown below.
using System; using System.Collections; class myHashTable { public static void Main() { // Create a Hashtable named phonetics Hashtable phonetics = new Hashtable(); // Add key/value pairs in phonetics phonetics.Add("A", "Apple"); phonetics.Add("B", "Banana"); phonetics.Add("C", "Cat"); phonetics.Add("D", "Dog"); phonetics.Add("E", "Elephant"); phonetics.Add("F", "Fish"); //print hahshtable collection Console.WriteLine("Hashtable items:"); foreach(DictionaryEntry entry in phonetics){ Console.WriteLine("{0} and {1} ", entry.Key, entry.Value); } string text = ""; IDictionaryEnumerator enumerator = phonetics.GetEnumerator(); while (enumerator.MoveNext()) { text += enumerator.Key + ", "; text += enumerator.Value + "
"; } Console.WriteLine("String value: {0}", text); } }
In this program, we have a hashtable of phonetics from A to F. We declare a string variable ‘text’. Then we declare an IDictioanaryEnumerator to traverse the hashtable. Then using this enumerator we copy the contents of the hashtable collection into the string using a ‘while’ loop.
Output
The program generates the following output.
Hashtable items: B and Banana C and Cat A and Apple F and Fish D and Dog E and Elephant String value: B, Banana C, Cat A, Apple F, FishD, Dog E, Elephant
Here, we have first displayed the contents of the hashtable collection. Next, we display the string. We can see the string value consists of all the elements of the hashtable separated by ‘
’ (newline).
Example
Let’s implement another program to convert the hashtable to a string. The complete C# program is given below.
using System; using System.Collections; class myHashTable { public static void Main() { // Create a Hashtable named numberNames Hashtable numberNames = new Hashtable(); // Add key/value pairs in numberNames numberNames.Add("10", "Ten"); numberNames.Add("20", "Twenty"); numberNames.Add("30", "Thirty"); numberNames.Add("40", "Forty"); numberNames.Add("50", "Fifty"); numberNames.Add("60", "Sixty"); //print hahshtable collection Console.WriteLine("Hashtable items:"); foreach(DictionaryEntry entry in numberNames){ Console.WriteLine("{0} => {1} ", entry.Key, entry.Value); } string text = ""; IDictionaryEnumerator enumerator = numberNames.GetEnumerator(); while (enumerator.MoveNext()) { text += enumerator.Key + " and "; text += enumerator.Value + "
"; } Console.WriteLine("String value: {0}", text); } }
This program is similar to the previous one except for minor changes in the way the hashtable and the string value are displayed.
Here we have a number names hashtable with keys as numbers and values as their corresponding number names. We then declare a IDictionaryEnumerator using which we traverse the hashtable and copy each item (key & value) of the hashtable to a string. Each hashtable item is separated by a delimiter ‘
’.
Output
The program generates the following output.
Hashtable items: 10 => Ten 60 => Sixty 40 => Forty 50 => Fifty 20 => Twenty 30 => Thirty String value: 10 and Ten 60 and Sixty 40 and Forty 50 and Fifty 20 and Twenty 30 and Thirty
As we can see from the above output, the hashtable collection items are successfully copied to the string variable.
So in this article, we have discussed converting a hashtable collection to a string. Though there is no direct method to do this conversion, we can easily convert the hashtable collection to a string by using the IDictionaryEnumerator. This holds the item (both key and value) of the hashtable and we can easily access the items and copy it to the string using a loop.
- Related Articles
- How to convert Dictionary to Hashtable in PowerShell?
- How to Convert Hashtable into an Array?
- How to convert JSON object to Hashtable format using PowerShell?
- How to convert the hashtable to the JSON format using PowerShell?
- How to convert command output to the Hashtable format in PowerShell?
- How to convert array to string PHP?
- How to convert String to Integer and Integer to String in Java?
- How to convert string to binary in Python?
- How to convert string to JSON using Python?
- How to convert Boolean to String in JavaScript?
- How to convert Number to String in JavaScript?
- How to convert String to Number in JavaScript?
- How to convert String to Boolean in JavaScript?
- How to convert int to String in java?
- How to convert String to Date in java?
