Skip to content

Instantly share code, notes, and snippets.

@simi
Created February 25, 2010 21:18
Show Gist options
  • Save simi/315045 to your computer and use it in GitHub Desktop.
Save simi/315045 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace serializeArray
{
class Program
{
static void Main(string[] args)
{
int[] c = new int[(int)char.MaxValue];
c[5] = 10;
c[6] = 20;
SerializeIntArray(c, "test.bin");
int[] d = DeserializeIntArray("test.bin");
Console.WriteLine(d[5]);
Console.WriteLine(d[6]);
}
public static void SerializeIntArray(int[] array, string filename)
{
FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
BinaryFormatter sf = new BinaryFormatter();
try
{
sf.Serialize(fs, array);
}
finally
{
fs.Close();
}
}
public static int[] DeserializeIntArray(string filename)
{
int[] arr;
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryFormatter sf = new BinaryFormatter();
try
{
arr = (int[])sf.Deserialize(fs);
}
finally
{
fs.Close();
}
return arr;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment