How to Serialize and Deserialize ArrayList in C#

1 Answer

0 votes
using System; // Console
using System.IO; // FileStream
using System.Collections; // ArrayList
using System.Runtime.Serialization; // IFormatter
using System.Runtime.Serialization.Formatters.Binary; // BinaryFormatter

namespace Serialization_b
{
	
    class Class1
	{
		static void Main(string[] args)
		{
			ArrayList al = new ArrayList();
			for (int i = 0; i < 10; i++)
				 al.Add (i);

            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream("d:\\foo.bin", FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, al);
            stream.Close();

            ArrayList al2 = new ArrayList();
            stream = new FileStream("d:\\foo.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
            al2 = (ArrayList)formatter.Deserialize(stream);
            stream.Close();

            foreach (int i in al2)
                  Console.WriteLine(i);
		}
	}
}

/* 
 
run:
 
0
1
2
3
4
5
6
7
8
9

*/



answered Sep 22, 2014 by avibootz

Related questions

2 answers 238 views
1 answer 171 views
2 answers 224 views
1 answer 113 views
1 answer 110 views
1 answer 103 views
1 answer 86 views
...