Working with the Serializable Attribute : Serialization : File Stream C# Source Code


Custom Search

C# Source Code » File Stream » Serialization »

 

Working with the Serializable Attribute








    

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
   
[Serializable]
class Point2D
{
    public int X;
    public int Y;
}
   
class MyMainClass
{
    public static void Main()
    {
        Point2D My2DPoint = new Point2D();
   
        My2DPoint.X = 100;
        My2DPoint.Y = 200;
   
        Stream WriteStream = File.Create("Point2D.bin");
        BinaryFormatter BinaryWrite = new BinaryFormatter();
        BinaryWrite.Serialize(WriteStream, My2DPoint);
        WriteStream.Close();
   
        Point2D ANewPoint = new Point2D();
   
        Console.WriteLine("New Point Before Deserialization: ({0}, {1})", ANewPoint.X, ANewPoint.Y);
        Stream ReadStream = File.OpenRead("Point2D.bin");
        BinaryFormatter BinaryRead = new BinaryFormatter();
        ANewPoint = (Point2D)BinaryRead.Deserialize(ReadStream);
        ReadStream.Close();
        Console.WriteLine("New Point After Deserialization: ({0}, {1})", ANewPoint.X, ANewPoint.Y);
    }
}
           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo File Stream
» Serialization