Serialize an ArrayList object to a binary file : ArrayList Serialization : Data Structure C# Examples


C# Examples » Data Structure » ArrayList Serialization »

 

Serialize an ArrayList object to a binary file









    
using  System;
using  System.IO;
using  System.Collections;
using  System.Runtime.Serialization.Formatters.Soap;
using  System.Runtime.Serialization.Formatters.Binary;

class  MainClass
{
        public  static  void  Main()
        {
                ArrayList  people  =  new  ArrayList();
                people.Add("G");
                people.Add("L");
                people.Add("A");


                BinarySerialize(people);

                ArrayList  binaryPeople  =  BinaryDeserialize();
                Console.WriteLine("Binary  people:");
                foreach  (string  s  in  binaryPeople)
                {
                        Console.WriteLine("\t"  +  s);
                }
        }
        
        private  static  void  BinarySerialize(ArrayList  list)
        {
                using  (FileStream  str  =  File.Create("people.bin"))
                {
                        BinaryFormatter  bf  =  new  BinaryFormatter();
                        bf.Serialize(str,  list);
                }
        }

        //  Deserialize  an  ArrayList  object  from  a  binary  file.
        private  static  ArrayList  BinaryDeserialize()
        {
                ArrayList  people  =  null;

                using  (FileStream  str  =  File.OpenRead("people.bin"))
                {
                        BinaryFormatter  bf  =  new  BinaryFormatter();
                        people  =  (ArrayList)bf.Deserialize(str);
                }
                return  people;
        }

}
    
   
  
   



Output

Binary people:
        G
        L
        A


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Data Structure
» ArrayList Serialization