Why am I unable to serialize and deserialize a List of Structs?
<p>I have a piece of code pestering me for long time and I have no way out. I'd like to simply serialize struct , then deserialize it.   </p>

<pre><code>[Serializable]
[StructLayout(LayoutKind.Sequential,Pack=1)]
struct Foo
{
    public byte Bar;            
    public Foo(byte b){Bar=b;}
}
List&lt;Foo&gt; foo=new List&lt;Foo&gt;;

void UpdateFoo(int i)
{
   Foo f=foo[i];
   WriteLine("{0}",(char)(f.Bar));
}
public static void Main (string[] args)
{
    Foo[] arr = new Foo[1000];
    for (int i = 0; i &lt; 1000; i++) {
        arr[i]=new Foo(42);            
    }
    var fmt = new BinaryFormatter();
    using(FileStream f= File.Create("test.bin")){
        fmt.Serialize(f,arr);
    }
    Console.WriteLine (new FileInfo("test.bin").Length);

    fmt=new BinaryFormatter();
    Foo f=new Foo();
    using(FileStream f= File.Open("test.bin")){
        f=fmt.DeSerialize(f);
        foo.Add(f);  
    }
    UpdateFoo(0);
}
</code></pre>

<p>The UpdateFoo in Main throws an exception as there is nothing in the list foo at all. </p>
