Click here to Skip to main content
15,885,537 members
Articles / Programming Languages / C#
Article

Hash Table and Serialization in .NET

Rate me:
Please Sign up or sign in to vote.
4.33/5 (12 votes)
7 Jul 20021 min read 175K   1.5K   39   11
This article introduces hash table and serialization in .NET

Sample Image - Phonebook.jpg

Introduction

This article introduces and describes the use of hash table and serialization in .NET using C#. The sample application used here is a Phone Book application. Phone Book application is a console application that allows a user to add, view, list, and delete entries containing names and phone numbers.

A hash table is a collection of key-value pairs. In .NET, the class that implements a hash table is the Hashtable class. Elements can be added to the hash table by calling the Add method passing in the key-value pairs that you want to add. The objects used as keys must implement

Object.Equals
and Object.GetHashCode methods.

C#
private Hashtable table = new Hashtable();
		
public void AddEntry(BookEntry entry)
{
   table.Add( entry.GetPerson(), entry );
}	

Once the hash table is populated, you can search and retrieve elements in it by calling the indexer for the Hashtable class.

C#
public BookEntry GetEntry(Person key)
{
   return (BookEntry) table[key];
}			

Entries can be removed from the hash table by calling the Remove method which takes a key identifying the element you want to remove.

C#
public void DeleteEntry(Person key)
{
   table.Remove( key );
}	

The populated hash table can be saved to a file by using serialization. Serialization is the process of converting an object into a linear sequence of bytes for either storage or transmission to another location. This task can be performed using BinaryFormater class which serializes the hash table object to the file stream.

C#
public void Save()
{
   Stream s = File.Open("Phone.bin", FileMode.Create, FileAccess.ReadWrite);
   BinaryFormatter b = new BinaryFormatter();
   b.Serialize(s, table);
   s.Close();      
}		

The hash table object can be recovered back from the file by using

Deserialize
method as shown below.

C#
s = File.Open("Phone.bin", FileMode.Open, FileAccess.Read);
BinaryFormatter b = new BinaryFormatter();
table = (Hashtable) b.Deserialize(s);

I hope you enjoy this brief introduction on hash table and serialization in .NET.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow can we deserialize the object that not matched with the original structrure of the object that has been serialized ? Pin
Budi Sentosa23-Feb-09 20:34
Budi Sentosa23-Feb-09 20:34 
Questionhw can i reset the integer "value" of key in a hash table using c#? Pin
merryjoy00023-Feb-09 20:05
merryjoy00023-Feb-09 20:05 
QuestionHow can i Clear only the values of a key in hash table using c# Pin
merryjoy00023-Feb-09 18:32
merryjoy00023-Feb-09 18:32 
Questionhas anyone found a Deserialization error and fixed it? Pin
desnu9-Jul-04 13:44
desnu9-Jul-04 13:44 
AnswerRe: has anyone found a Deserialization error and fixed it? Pin
hack.root5-Dec-05 15:04
hack.root5-Dec-05 15:04 
GeneralRemoving within a ForEach not possible Pin
A Berglas6-Jun-03 8:10
A Berglas6-Jun-03 8:10 
GeneralRe: Removing within a ForEach not possible Pin
leppie6-Jun-03 9:00
leppie6-Jun-03 9:00 
aberglas wrote:
How do you make the following code work correctly?

You dont!

aberglas wrote:
Think about the hard questions

Yes, try do that next time. The article is a beginners article, nothing wrong with that. Your question on the other hand is totally off topic. If you did infact read thru MSDN, you would have noticed that a collection is NOT modifiable via its Enumarator (which a foreach is infact).

But here follows an answer anyways:

Best will be to make a copy of the key values then foreach thru that and remove the entries like that.

<a TITLE="See my user info" href=http://www.codeproject.com/script/profile/whos_who.asp?id=38829>leppie<a>::<a TITLE="Go to all articles  written by me" href=http://www.codeproject.com/script/articles/list_articles.asp?userid=38829>AllocCPArticle</a>(<a TITLE="Go to my latest, greatest article!" href=http://www.codeproject.com/useritems/dfamachine.asp >Generic DFA State Machine for .NET</a>);

AnswerRe: Removing within a ForEach not possible Pin
hack.root5-Dec-05 18:48
hack.root5-Dec-05 18:48 
Question"^" What does this mean ? Pin
Cypher21-Feb-03 0:11
Cypher21-Feb-03 0:11 
AnswerRe: "^" What does this mean ? Pin
Liong Ng23-Feb-03 10:45
Liong Ng23-Feb-03 10:45 
GeneralWhile your at it, explain the HCP Pin
TigerNinja_27-Nov-02 15:03
TigerNinja_27-Nov-02 15:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.