Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

C# Hashtables [Intermediate Level Tutorial]

C# Hashtables


What Is It:
A Hashtable, unlike an array, will allow you to store data based on keys and values. Each key will have a value. You can use this to store Names, Numbers, anything in your life that would require organization and order! A HashTable is known as an associative array.


Getting Started:
The very first thing we want to do when working with a Hashtable..is to set it up!

Code
Hashtable hf = new Hashtable();

By reading this code you should have a fairly basic understanding of what is does. It creates a new Hashtable object that is named "hf"


The next thing we want to do is start setting up our keys/values.

Code
hf["Predictable"] = "is cool";
hf["Hackforums"] = "is cooler";

hf is obviously the name of our Hashtable. "Predictable" is one of the keys in our Hashtable, it is obviously named Predictable!

After the equals sign is our value, the value is the stuff that our key is holding.


Now, we want to display our key/value pairs in a MessageBox, Right?

NO! MessageBox's are getting boring. So, create a new listbox. A listbox is just a component that will display a list of something. To display our keys and values in our new listbox, we do the following:

Code
foreach (DictionaryEntry h in hf)
            {
                listBox1.Items.Add(h.Key + " " + h.Value);

            }


This is called a foreach loop. We use the word "DictionaryEntry" is what we use when using a foreach loop with a Hashtable, "h" is just the name of our new variable.

We use "listbox1.Items.Add" to add something to the listbox. "h.Key" and "h.Value" simply adds the key/value pairs to our listbox!


Conclusion:
Hashtables can be used for anything, whether it is storing people in your contacts, matching MD5 hashes with plaintext, or simply screwing around.

[IMG]http://i52.[censored].com/302xdl1.jpg[/IMG]


Code:
Code
private void button1_Click(object sender, EventArgs e)
        {
            Hashtable hf = new Hashtable();


            hf["Predictable"] = "is cool";
            hf["Hackforums"] = "is cooler";


            foreach (DictionaryEntry h in hf)
            {
                listBox1.Items.Add(h.Key + " " + h.Value);

            }
        }
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
I wouldn't start use hashtables like this, but thats ur way in coding style.
Maybe u should start explain about certain arrays and other collection types.
You could have done this much easier, but good guide anyway :)
(Oct 22, 2010 01:19 PM)BaussHacker Wrote:  I wouldn't start use hashtables like this, but thats ur way in coding style.
Maybe u should start explain about certain arrays and other collection types.
You could have done this much easier, but good guide anyway :)

Thanks a lot for the constructive criticism. I will write a better, more effective tut in the future.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
Explain Hashtable's run-time efficiency compared to arrays and such.
all types of arrays should be declared after use, else they can send alot random null reference exceptions.
hashtables can keep the data for longer time + they can keep more.
Well I dont wanna go deeper in it, if u dont want me to, then I will explain it total out, because this wont explain much, if u dont know about it already.
(Oct 22, 2010 04:13 PM)BaussHacker Wrote:  all types of arrays should be declared after use, else they can send alot random null reference exceptions.
hashtables can keep the data for longer time + they can keep more.
Well I dont wanna go deeper in it, if u dont want me to, then I will explain it total out, because this wont explain much, if u dont know about it already.

No. Just no...
🤦‍♂🤦‍♂🤦‍♂🤦‍♂
Example on arraylist.
Code
static void Main(string[] args)
  {
    //Use ararylists temp only, else u will get random errors (null exception eg.)
    //Make sure u copy to a real array, because arraylists are to hold less data
    ArrayList list = new ArrayList();
    Random random = new Random();
    for (int i = 0; i < random.Next(0, 1000); i++)
    {
    list.Add(i);
    }
    int[] buffer = new int[list.Count];
    list.CopyTo(buffer);
    foreach (int i2 in buffer)
    {
    Console.WriteLine(i2);
    }
  }

more u want?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by dummycartSep 9, 2021 03:38 AM 1 146 Sep 9, 2021 03:38 AM
Last Post: Lindros
[Python] by CiataLearnerDec 22, 2018 02:27 PM 1 211 Dec 22, 2018 02:27 PM
Last Post: notjapanese
[C] by Adam2017Dec 3, 2018 03:06 PM 2 167 Dec 3, 2018 03:06 PM
Last Post: Adam2017