With C# (and many other languages) you can serialize (aka save) the state of a objects directly to a file and de-serialize (aka re-instantiate/load) them later so that you have the same objects with the same state.
This works best if you have a prober scenegraph because this way it will happen almost automatically in C#.
As an example say I have the following scenegraph
Root : MyRootClass
-Score : int
-WorldObjects : List<MyGameObject>
--Paddle : MyPlaceClass
--Ball : MyBallClass
--Blocks : List<MyBLockClass>
---BlockA : MyBlockClass
---BlockB : MyBlockClass
---BLockEtc
You should decorate all of the above classes with the [Serializable()] attribute and any fields in them which you do not wish to save or load (say references to a graphics device or something) should be marked with the [NonSerializable()] attribute
You can then use the BinaryFormatter to serialize ONLY the the root object to a file, since all other elements are referenced by the root object they will also be serialized.
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
// Construct a BinaryFormatter and use it to serialize the data to the stream.
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, root);
}
catch (SerializationException e)
{
Console.WriteLine("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
In the same fashion you can load the root object again:
FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
try
{
BinaryFormatter formatter = new BinaryFormatter();
// Deserialize the hashtable from the file and
// assign the reference to the local variable.
addresses = (Hashtable) formatter.Deserialize(fs);
}
catch (SerializationException e)
{
Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
Now if you're lucky all the objects that you wanted to serialize only references objects that can be serialized, if you're not so lucky you need a way to fix those references, for example by calling some custom Reinitialize method that gives the object enough info so that they can reconstructed missing references themselves. A good example of this is passing them a new references to the graphics device.