Hach table and serialization in .NET
Cowboy compile
chnenzizhao@hotmail.com
2004.4.21
Introduction
This article describes, in .NET, use hash tables and serial C # usage. The sample application used here is
A phone book. The phone book application is a program for a console. It allows you to add, view, list and delete it
The name and phone number inside.
The Harneous Table is a collection of "key-value" pairs. In .NET, class hashtable is the implementation of the hash table. through
The Add method is transferred to pass the key value pair you want to add, you can complete the addition. These objects used as a key
It is necessary to implement Object.Equals and Object.gethashcode methods.
Private hashtable table = new hashtable ();
Public Void Addentry (Bookentry Entry)
{
Table.Add (entry.getPerson (), Entry);
}
After the Harlemen table is built, you can retrieve these members by calling the HashTable class.
Public Bookentry GeTENTRY (Person Key)
{
Return (BookenTry) Table [Key];
}
You can remove the entry by calling the REMOVE method. Here, use the key to distinguish the entries to be removed.
Public void deletentry (Person Key)
{
Table.Remove (key);
}
By serialization, we can save this hazard table to the file. Serialization is to convert objects into linear bytes
Columns to store them in a storage device or transferred to other places, This task can be made from binary
Formater class is done. It serials the Harnes table object into a 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 ();
}
As shown in the following, the Harnes table object can be converted back by calling the Deserialize method.
s = file.open ("Phone.bin", FileMode.Open, FileAccess.Read;
Binaryformatter b = new binaryformatter ();
Table = (hashtable) B.deSerialize (s);
I hope this brief introduction to .NET is a simple introduction to you.