title: Is there a better way of serializing objects in C#?
body: <p>One of my colleagues challenged me to put this code up here to see if anyone can think of a better way of doing this.</p>
  
  <p>It's two methods to serialize and load objects from disc. The load uses a generic parameter to specify the type of object that is being loaded.</p>
  
  <pre><code>public static class IO
  {
      public static void SaveObject(string path, object obj)
      {
          using (Stream stream = File.Open(path, FileMode.Create))
          {
              IFormatter formatter = new BinaryFormatter();
              formatter.Serialize(stream, obj);
          }
      }
  
      public static T LoadObject&lt;T&gt;(string path)
      {
          using (Stream stream = File.Open(path, FileMode.Open))
          {
              IFormatter formatter = new BinaryFormatter();
              T obj = (T)formatter.Deserialize(stream);
              return obj;
          }
      }
  }
  </code></pre>