|
Serialization |
|
|
|
As a simplest example of this we created an array of objects of class emp. We stored this data in a .dat file on the secondary storage and retrieved it using deserialization. Here is how the program will look namespace serialize using System; int id; id = i ; } public string ToString() return id + " " + name + " " + salary ; } }
public class Class1 public static int Main ( string[] args ) emp[] e = new emp ( 1, "Rahul", 4500 ), } ;
File f = new File ( "C:\\emp.dat" ) ; b.Serialize ( s, e[i] ) ; }
emp ee = (emp)b.Deserialize(s); } s.Close(); } } } The Serializable attribute indicates that a class can be serialized. Here we intend to serialize the
emp class so we should write it above the class definition. We created a new file
emp.dat in the C drive and dumped the emp objects in it. Next we opened the same file and used the information for displaying. The
BinaryFormatter class provides a way to serialize and deserialize an object, or an entire graph of connected objects; it uses a binary format for the serialized stream, which is both very compact, and fast to parse. Indicates that a class can be serialized |