C# .NET

C# Collections – Hashtables

 

 

You use an Hashtable when you want to store information
based on Key/Value pairs. For example, the name of a student AND the score in
an exam. This allows you to mix text and numbers. (In other programmes, Hashtables
are known as associative arrays.)

Add a new button to your form, and double click it to get at the code. Now
have a look at the using statements at the top. Add the following to the end
of the list:

using System.Collections;

You already have a System.Collections.Generic statement, but a Hastable
is not part of this collection – it’s part of the normal Collections
namespace.

Click inside the code stub for you button. You setting up the Hashtable like
this:

Hashtable students = new Hashtable();

This creates a new object called students. It’s going to be an Hashtable
object.

There are two ways you can add data to your Hashtable. Like this:

students[“Jenny”] = 87;
students[“Peter”] = “No Score”;
students[“Mary Jane”] = 64;
students[“Azhar”] = 79;

Or like this:

students.Add(“Jenny”, 87);
students.Add(“Peter”, “No Score”;);
students.Add(“Mary Jane”, 64);
students.Add(“Azhar”, 79);

The first method uses a pair of square brackets:

students[“Jenny”] = 87;

In between the square brackets, you type what’s known as the Key. So this particular
entry in the Hashtable is called “Jenny”. After an equals sign, you
then type the Value that this Key will hold. Notice that three of the entries
are number values, and one (Peter) is text.

The second way to store values in an Hashtable is to use the Add Method:

students.Add(“Jenny”,
87);

In between the round brackets of Add( ), you first type the Key name.
After a comma, you type the Value for that Key.

There is a difference between the two. If you use the Add method, you can’t
have duplicate Key names. But you can if you use the square brackets. So this
will get you an error:

students.Add(“Jenny”, 87);
students.Add(“Jenny”, 35);

But this won’t:

students[“Jenny”] = 87;
students[“Jenny”] = 35;

To try Hashtables out for yourself, add the following code to your button:

Hashtable students = new Hashtable();

students[“Jenny”] = 87;
students[“Peter”] = “No Score”;
students[“Mary Jane”] = 64;
students[“Azhar”] = 79;

foreach (DictionaryEntry child in students)
{

listBox1.Items.Add(“student: ” + child.Key + ”
, Score: ” + child.Value);

}

Before running the code, have a look at the foreach loop. Inside of the round
brackets, we have this:

DictionaryEntry child

This sets up a variable called child. But note the type of variable it is:
a DictionaryEntry. C# uses an object of this type when using the foreach
loop with Hashtables. That’s because it automatically returns both the Key and
the Value.

Notice, too, what we have between the round brackets of the listbox’s Add method:

“student: ” + child.Key + ” , Score: ”
+ child.Value

The red bold are the important parts. After typing the name of your variable
(child, for us) and a full stop, the IntelliSense list will appear. Key
is a property that returns the name of your Key, and Value is a property that
returns whatever you placed inside of that Key.

Run your programme and click your button. The form on your listbox will then
look like this:

HashTables in C# NET

But just like the List, you can Add new items, and remove old ones. To Remove
an item, you do it like this:

students.Remove(“Peter”);

So you refer to the Key name, and not the Value, when you use the Remove method.

In the next lesson, we’ll take a look at Enumerations.

|

Kaynak : https://www.homeandlearn.co.uk/csharp/csharp_s7p7.html ‘sitesinden alıntı

Yorum Yap