SerializationException: Value has been serialized already

I implemented the ISerializable interface on my class so I serialize my datas as follows:

[Serializable]
public class MyClass: ISerializable
{
   public MyClass() {//default constructor}
   public int foo = 5;
   public GetObjectData(SerializationInfo info, StreamingContext context)
   {
        info.AddValue("foo",foo,typeof(int));
   }
}

This compiles whitout errors. But then I decide that I want to add an float member to MyClass class, so now my definition for MyClass is :

[Serializable]
public class MyClass: ISerializable
{
   public MyClass() {//default constructor}
   public int foo = 5;
   public float ffoo = 3.14;   //the added member
   public GetObjectData(SerializationInfo info, StreamingContext context)
   {
        info.AddValue("foo",foo,typeof(int)); 
        info.AddValue("ffoo",ffoo,typeof(float));
   }
}

But now the SerializationException is thrown saying that the value (foo) variable was already serialized. So how can I avoid this behavior? Because I am sure that later on I will add more members to my class which need to be serialized.

Well, you add the value “mACount” twice to the context. Once in line 14 and again in line 32.

You also read it twice. It seems that the value you store it in “c” isn’t used at all.