how to print hashtable in c#

how to print hashtable in c#


How to Print Hashtable in C#

In C#, a Hashtable is a collection that stores key-value pairs. Once populated, you can print the contents of the Hashtable to verify its data or provide useful output. This article will guide you through the process of printing a Hashtable in C# using different techniques and best practices.

Printing Methods

Iterating Through Hashtable with Foreach

The Hashtable class implements the IEnumerable interface, allowing iteration through each key-value pair using a foreach loop. Here’s how you can print the entire Hashtable:

 

using System;
using System.Collections;

public class HashtablePrintExample
{
    public static void Main()
    {
        // Create and populate a hashtable
        Hashtable studentScores = new Hashtable
        {
            { "Alice", 95 },
            { "Bob", 88 },
            { "Charlie", 78 },
            { "Diana", 85 }
        };

        // Print each key-value pair using a foreach loop
        Console.WriteLine("Student Scores:");
        foreach (DictionaryEntry entry in studentScores)
        {
            Console.WriteLine($"{entry.Key}: {entry.Value}");
        }
    }
}

Accessing Values by Key

You can also print individual values by accessing them directly via their keys:

 

using System; using System.Collections; public class HashtableSinglePrintExample {    public static void Main()    {        // Create and populate a hashtable        Hashtable countries = new Hashtable        {            { "USA", "Washington, D.C." },            { "UK", "London" },            { "Japan", "Tokyo" },            { "India", "New Delhi" }        };        // Print specific values by key        Console.WriteLine($"Capital of USA: {countries["USA"]}");        Console.WriteLine($"Capital of Japan: {countries["Japan"]}");    } }

Formatting Output with String Methods

To make the printed output more readable, use string formatting methods:

using System;
using System.Collections;

public class HashtableFormattedPrintExample
{
    public static void Main()
    {
        // Create and populate a hashtable
        Hashtable products = new Hashtable
        {
            { "P001", "Laptop" },
            { "P002", "Smartphone" },
            { "P003", "Tablet" }
        };

        // Print the formatted output
        Console.WriteLine("Product List:");
        foreach (DictionaryEntry product in products)
        {
            Console.WriteLine($"Product ID: {product.Key}, Name: {product.Value}");
        }
    }
}

Best Practices for Printing Hashtable

  • Order Consistency: Be aware that Hashtable doesn't maintain order.
  • Null Handling: Handle null keys and values gracefully during printing.
  • String Interpolation: Use string interpolation or formatting for cleaner output.

Conclusion

Printing the contents of a Hashtable in C# is straightforward using loops or direct key access. Following best practices will help ensure clear and accurate output, making it easier to verify and share the collection's data.


 

Leave a reply Your email address will not be published. Required fields are marked*

Popular Posts

Categories Clouds