@aAsCArZ-| 5WC- 68888778774VWBXCCBZZ-

List, serialize.` A List can be serialized to the disk. We want to serialize (to a file) a List of objects. The next time the program runs, we get this List straight from the disk. We see an example of BinaryFormatter and its Serialize methods. `List `list`Example.` This is the first part of the code example. We see a class in C# code, which you will want to put in a file called Lizard.cs. But you can also just put it in the same file. It adds several important namespaces in the using-statements. `We see` a class called Lizard, and it has three automatic properties. These store values and also are properties so are publicly accessible. The first constructor in the example accepts three values, a string, an int and a bool. `The Serializable attribute is specified right before the class definition.`Note 2: `It tells the .NET Framework that the properties on this class can be written to a file and read back from.`Example 2.` The second part of this tutorial is the Main method in your C# console program. It allows you to easily see how the data file is written to with BinaryFormatter, and how it is read from, deserialized. `This code` is mostly a command line program that allows you to type "s" to write a List of classes to a file, and "r" to read in that same List. It is fun to test the program in a console project.`When you press` "s", a new List of Lizard objects is created. Five different Lizard objects are instantiated. Next, we wrap the file IO code in a try-catch block. This is important because file IO frequently throws. `The Stream is wrapped in a using block. The File.Open call attempts to open the new file for writing.`File.Open `file-open`New BinaryFormatter instance.` We simply call the Serialize method on the BinaryFormatter instance. This Serialize method receives the stream you want to write to, and also the object itself.`Deserialize.` It again must use a Stream, which is wrapped in a using block for maximum efficiency and reliability. A new BinaryFormatter object is created, and it is used to get a new List<Lizard>. `The Deserialize method, which accepts the Stream as a parameter, is slow but also powerful.`Finally: `We write the List of Lizards it has read in from the file to the screen in a foreach-loop.`Foreach `foreach`Properties.` It is important to use properties, which have the get and set keywords, in this sort of serialization code. Properties have special metadata in the compiled code, which allows the base class library to use sophisticated logic. `Property `property`XML.` In this simple lizard example, using XML serialization might be best. This would generate a human-readable and editable data file, and would be more interoperable with web services. `XML `html`Summary.` We saw a powerful way to serialize binary data to a file. We took a List generic instance with five objects in it, and serialized it efficiently. We employed the using statement for optimal clarity of the Stream code.

567 6P6O; 6 6P6O.IO; 6P6O.Runtime.Serialization.Formatters.Binary6S7[Serializable()]7 6=6DLizard { 56=6L Type { get; set; } 56=6sNumber { get; set; } 56=bool Healthy { get; set; } 56=Lizard(6L t, 6sn, bool h) 5{ 55Type6z t; 55Number6z n; 55Healthy6zh; 5} }7 6P6O; 6 6P6O.IO; 6P6O.Runtime.Serialization.Formatters.Binary;7 6{[Note: You can paste the Lizard 6Dhere] <-- 76D6; { 56$5{ 556g(true) 55{6'6%7"s=serialize, r=read:"7);6'6N (64.6xLine())6'{6'56V7"s"7:6'55var lizards16z6w6u<Lizard>();6'55lizards1.6n6wLizard(7"Thorny devil"7,55551, true));6'55lizards1.6n6wLizard(7"Casquehead lizard"7,55 0, false));6'55lizards1.6n6wLizard(7"Green iguana"7,55554, true));6'55lizards1.6n6wLizard(7"Blotched blue-tongue lizard"7, 0, false));6'55lizards1.6n6wLizard(7"Gila monster"7,55551, false))6S55555try6'55{6'5556P(6K stream6zFile.Open(7"data.bin"7, FileMode.C6`))6'555{6'55557BinaryFormatter7 bin6z6wBinaryFormatter();6'5555bin.7Serialize7(stream, lizards1);6'555}6'55}6'55catch (IO6))6'55{6'55}6'556C 6'56V7"r"7:6'55try6'55{6'5556P(6K stream6zFile.Open(7"data.bin"7, FileMode.Open))6'555{6'55557BinaryFormatter7 bin6z6wBinaryFormatter()6S5555555var lizards26z(6u<Lizard>)bin.7Deserialize7(stream);6'555567 (Lizard lizard6klizards2)6'5555{6'555556%7"{0}, {1}, {2}"7,6'555555lizard.Type,6'555555lizard.Number,6'555555lizard.Healthy);6'5555}6'555}6'55}6'55catch (IO6))6'55{6'55}6'556C6'} 55} 5} } 7 s=serialize, r=read: s s=serialize, r=read: r Thorny devil, 1, True Casquehead lizard, 0, False Green iguana, 4, True Blotched blue-tongue lizard, 0, False Gila monster, 1, False s=serialize, r=read:7

;describes serializable type3serializes the type