5,533,615 members and growing! (17,472 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate

Hash Table and Serialization in .NET

By Liong Ng

This article introduces hash table and serialization in .NET
C#, Windows, .NET 1.0, .NET, Visual Studio, Dev

Posted: 7 Jul 2002
Updated: 7 Jul 2002
Views: 93,737
Bookmarked: 30 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
12 votes for this Article.
Popularity: 3.85 Rating: 3.57 out of 5
1 vote, 12.5%
1
2 votes, 25.0%
2
0 votes, 0.0%
3
2 votes, 25.0%
4
3 votes, 37.5%
5

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.

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.

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.

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.

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.

    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

About the Author

Liong Ng



Occupation: Web Developer
Location: United States United States

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
Subject  Author Date 
Generalhas anyone found a Deserialization error and fixed it?memberdesnu14:44 9 Jul '04  
GeneralRe: has anyone found a Deserialization error and fixed it?memberhack.root16:04 5 Dec '05  
GeneralRemoving within a ForEach not possiblememberaberglas9:10 6 Jun '03  
GeneralRe: Removing within a ForEach not possiblememberleppie10:00 6 Jun '03  
AnswerRe: Removing within a ForEach not possiblememberhack.root19:48 5 Dec '05  
General"^" What does this mean ?memberCypher1:11 21 Feb '03  
GeneralRe: "^" What does this mean ?memberLiong11:45 23 Feb '03  
GeneralWhile your at it, explain the HCPmemberSoliant16:03 27 Nov '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Jul 2002
Editor: Chris Maunder
Copyright 2002 by Liong Ng
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project