-3

I have two questions:

  1. What is the difference between a Hashtable and a Dictionary?

  2. Is there any possible way to save either of these collections to disk?

Newell Clark
  • 345
  • 2
  • 13
InvBoy
  • 79
  • 1
  • 1
  • 6
  • Please use MSDN... – Mat Jan 16 '17 at 08:07
  • To give you a great answer, it might help us if you have a glance at [ask] if you haven't already. It might be also useful if you could provide a [mcve]. – Mat Jan 16 '17 at 08:07
  • @Mat you are right but i just need to know the answer of second question( when i add item's....) – InvBoy Jan 16 '17 at 08:10
  • Question suggests Op hasn't the foggiest idea about the concepts asked. Should read: [C# Hashtable Examples](https://www.dotnetperls.com/hashtable), [Difference between Dictionary and Hashtable](http://stackoverflow.com/questions/876656/difference-between-dictionary-and-hashtable) and [Why is Dictionary preferred over hashtable?](http://stackoverflow.com/questions/301371/why-is-dictionary-preferred-over-hashtable) before amending and/or reposting the question. – Serge Jan 16 '17 at 09:08
  • Also see [How does the process of hashing work in Dictionary](http://stackoverflow.com/questions/1407563/how-does-the-process-of-hashing-work-in-dictionarytkey-tvalue). – Martin Liversage Jan 16 '17 at 09:22
  • Also briefly in Documentation - https://stackoverflow.com/documentation/c%23/2344/an-overview-of-c-sharp-collections/7709/hashsett – Matthew Steeples Jan 16 '17 at 09:24

1 Answers1

2

First of all, don't use a Hashtable. Use a HashSet instead. You can find it in the namespace System.Collections.Generic.

What is a Hash Map?

A Hash map (or Dictionary, as it's called in C#) is a data structure that allows you to look up one type of data by using another type of input. Basically, when you add an item to the dictionary, you specify both a key and a value. Then when you want to look up the value in the dictionary, you simply give it the key and it will give you the value that is associated with it.

For example, if you have a bunch of Product objects that you want to be able to look up by their UPCs, you would add the products to your Dictionary with the Product as the value and the UPC number as the key.

A HashSet on the other hand, does not store pairs of keys and values. It simply stores items. A hash set (or any set, for that matter) ensures that when you add items to the collection, there will be no duplicates.

When I Add Item's In Hash Table , Can I Save It As New File And Restore The Original Item's?

First of all, don't use a Hashtable. Use a HashSet instead. You can find it in the namespace System.Collections.Generic. To use it, you simply add items to it the same way you do with any other collection.

Like the other collections, the HashSet supports Serialization (Serialization is when you take an object and convert it to a string of bytes so it can be saved to a file or sent over the internet). Here's a sample program that shows serialization of a hash-set:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace HashSetSerializationTest
{
class Program
{
    static void Main(string[] args)
    {
        var set = new HashSet<int>();
        set.Add(5);
        set.Add(12);
        set.Add(-50006);

        Console.WriteLine("Enter the file-path:");
        string path = Console.ReadLine();
        Serialize(path, set);
        HashSet<int> deserializedSet = (HashSet<int>)Deserialize(path);

        foreach (int number in deserializedSet)
        {
            Console.WriteLine($"{number} is in original set: {set.Contains(number)}");
        }
        Console.ReadLine();
    }

    static void Serialize(string path, object theObjectToSave)
    {
        using (Stream stream = File.Create(path))
        {
            var formatter = new BinaryFormatter();
            formatter.Serialize(stream, theObjectToSave);
        }
    }

    static object Deserialize(string path)
    {
        using (Stream stream = File.OpenRead(path))
        {
            var formatter = new BinaryFormatter();
            return formatter.Deserialize(stream);
        }
    }
}
}

In order to serialize anything, you'll need to include System.IO and System.Runtime.Serialization.Formatters.Binary.

Newell Clark
  • 345
  • 2
  • 13