📄 class1.cs
字号:
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace Serialize
{
[Serializable]
public class Insect
{
private string name;
[NonSerialized]
private int id;
public Insect(string name, int id)
{
this.name = name;
this.id = id;
}
public override string ToString()
{
return String.Format("{0}: {1}", name, id);
}
}
class SerializeApp
{
[STAThread]
public static void Main(string[] args)
{
// Serialize one Insect
Insect i = new Insect("Meadow Brown", 12);
Stream sw = File.Create("Insects.bin");
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(sw, i);
sw.Close();
// Serialize a collection of Insects
ArrayList box = new ArrayList();
box.Add(new Insect("Marsh Fritillary", 34));
box.Add(new Insect("Speckled Wood", 56));
box.Add(new Insect("Milkweed", 78));
sw = File.Open("Insects.bin", FileMode.Append);
bf.Serialize(sw, box);
sw.Close();
// Deserialize one Insect
Stream sr = File.OpenRead("Insects.bin");
Insect j = (Insect)bf.Deserialize(sr);
Console.WriteLine(j);
// Deserialize the collection
ArrayList bag = (ArrayList) bf.Deserialize(sr);
sr.Close();
foreach (Insect k in bag)
{
Console.WriteLine(k);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -