Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5.  
  6. // [Note: You can paste the Lizard class here] <--
  7.  
  8. class Program
  9. {
  10.     static void Main()
  11.     {
  12.         while(true)
  13.         {
  14.             Console.WriteLine("s=serialize, r=read:");
  15.             switch (Console.ReadLine())
  16.             {
  17.                 case "s":
  18.                     var lizards1 = new List<Lizard>();
  19.                     lizards1.Add(new Lizard("Thorny devil",                1, true));
  20.                     lizards1.Add(new Lizard("Casquehead lizard",           0, false));
  21.                     lizards1.Add(new Lizard("Green iguana",                4, true));
  22.                     lizards1.Add(new Lizard("Blotched blue-tongue lizard", 0, false));
  23.                     lizards1.Add(new Lizard("Gila monster",                1, false));
  24.  
  25.                     try
  26.                     {
  27.                         using (Stream stream = File.Open("data.bin", FileMode.Create))
  28.                         {
  29.                             BinaryFormatter bin = new BinaryFormatter();
  30.                             bin.Serialize(stream, lizards1);
  31.                         }
  32.                     }
  33.                     catch (IOException)
  34.                     {
  35.                     }
  36.                     break;
  37.  
  38.                 case "r":
  39.                     try
  40.                     {
  41.                         using (Stream stream = File.Open("data.bin", FileMode.Open))
  42.                         {
  43.                             BinaryFormatter bin = new BinaryFormatter();
  44.  
  45.                             var lizards2 = (List<Lizard>)bin.Deserialize(stream);
  46.                             foreach (Lizard lizard in lizards2)
  47.                             {
  48.                                 Console.WriteLine("{0}, {1}, {2}",
  49.                                     lizard.Type,
  50.                                     lizard.Number,
  51.                                     lizard.Healthy);
  52.                             }
  53.                         }
  54.                     }
  55.                     catch (IOException)
  56.                     {
  57.                     }
  58.                     break;
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement