fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.Runtime.InteropServices;
  8.  
  9. namespace Test
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. int data1 = 42;
  16. double data2 = 2.12;
  17. string data3 = "Hello, World!";
  18.  
  19.  
  20. byte[] bytes = null;
  21.  
  22. BinaryFormatter bf = new BinaryFormatter();
  23. using(MemoryStream ms = new MemoryStream())
  24. {
  25. bf.Serialize(ms, data1);
  26. bf.Serialize(ms, data2);
  27. bf.Serialize(ms, data3);
  28. bytes = ms.ToArray();
  29. }
  30.  
  31. Console.WriteLine("{0} bytes", bytes.Length);
  32.  
  33.  
  34. bf = new BinaryFormatter();
  35. using(MemoryStream ms = new MemoryStream(bytes))
  36. {
  37. object obj = bf.Deserialize(ms);
  38. Console.WriteLine(data1 == (int)obj);
  39.  
  40. obj = bf.Deserialize(ms);
  41. Console.WriteLine(data2 == (double)obj);
  42.  
  43. obj = bf.Deserialize(ms);
  44. Console.WriteLine(data3 == (string)obj);
  45. }
  46. }
  47. }
  48. }
Success #stdin #stdout 0.04s 16312KB
stdin
Standard input is empty
stdout
150 bytes
True
True
True