Empty List hence an exception
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.   
 

    [Serializable]
    [StructLayout(LayoutKind.Sequential,Pack=1)]
    struct Foo
    {
        public byte Bar;            
        public Foo(byte b){Bar=b;}
    }
    List<Foo> foo=new List<Foo>;
    
    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 < 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);
    }
The UpdateFoo in Main throws an exception as there is nothing in the list foo at all. 

Empty List hence an exception
===

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.   
 

    [Serializable]
    [StructLayout(LayoutKind.Sequential,Pack=1)]
    struct Foo
    {
        public byte Bar;            
        public Foo(byte b){Bar=b;}
    }
    List<Foo> foo=new List<Foo>;
    
    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 < 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);
    }
The UpdateFoo in Main throws an exception as there is nothing in the list foo at all. 
