C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Properties: We see a class called Lizard, and it has 3 automatic properties. These store values and also are properties so are publicly accessible.
PropertyConstructor: The first constructor in the example accepts 3 values, a string, an int and a bool.
ConstructorNote: The Serializable attribute is specified right before the class definition.
AttributeNote 2: It tells the .NET Framework that the properties on this class can be written to a file and read back from.
C# program that describes serializable type
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable()]
public class Lizard
{
public string Type { get; set; }
public int Number { get; set; }
public bool Healthy { get; set; }
public Lizard(string t, int n, bool h)
{
Type = t;
Number = n;
Healthy = h;
}
}
Program: This code 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.
List: When you press "s", a new List of Lizard objects is created. Five different Lizard objects are instantiated.
Try: Next, we wrap the file IO code in a try-catch block. This is important because file IO frequently throws.
And: The Stream is wrapped in a using block. The File.Open call attempts to open the new file for writing.
File.OpenC# program that uses Serialize on the type
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
// [Note: You can paste the Lizard class here] <--
class Program
{
static void Main()
{
while(true)
{
Console.WriteLine("s=serialize, r=read:");
switch (Console.ReadLine())
{
case "s":
var lizards1 = new List<Lizard>();
lizards1.Add(new Lizard("Thorny devil", 1, true));
lizards1.Add(new Lizard("Casquehead lizard", 0, false));
lizards1.Add(new Lizard("Green iguana", 4, true));
lizards1.Add(new Lizard("Blotched blue-tongue lizard", 0, false));
lizards1.Add(new Lizard("Gila monster", 1, false));
try
{
using (Stream stream = File.Open("data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, lizards1);
}
}
catch (IOException)
{
}
break;
case "r":
try
{
using (Stream stream = File.Open("data.bin", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
var lizards2 = (List<Lizard>)bin.Deserialize(stream);
foreach (Lizard lizard in lizards2)
{
Console.WriteLine("{0}, {1}, {2}",
lizard.Type,
lizard.Number,
lizard.Healthy);
}
}
}
catch (IOException)
{
}
break;
}
}
}
}
Output
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:
Note: 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