Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Serialization and instances in C#

Hi y'all. 

I'm just entering C# and I have my first questions about serialization. 

Let's suppose I have a simple class:

public class A
{
    int a;
    string b;
}

And then I have another class:

public class B
{
    int c;
    string d;
    A a1;
    A a2;
    A a3;
    A a4;
}

Now, what happens in runtime if I have something like this (I've left out accesors for simplicity)

o1 = new A();
o2 = new A();

b1 = new B();
b1.c = 3;
b1.d = 'Hello';
b1.a1 = o1;
b1.a2 = o2;
b1.a3 = o1;
b1.a4 = o2;

Which means: I've created 2 'A' instances, and a 'B' instance.
B's instances of A are references to two objects. This is ok.

Now, what would happen if I serialize to a file and then deserialize in another program.  After inspecting the generated XML files my best guess is that deserialization will create a total of 4 instances of A, being a1 and a3 identical -and the same for a2 and a4-. 

The semantics will be ok, but isn't this unefficient?

Epaminondas Pantulis
Tuesday, January 21, 2003

Sorry, I can't answer your question, but I'd like to notice that semantics WILL NOT BE OK. Any change of some A object will result in inconsistency of your system.

Mikhail Andronov
Tuesday, January 21, 2003

Your assumption is correct. If You want two A objects instead of four, You have to store Your own indexes. That means referencing to an array of A objects.

Nordic Light
Tuesday, January 21, 2003

Thank you both. 

Epaminondas Pantulis
Friday, January 24, 2003

*  Recent Topics

*  Fog Creek Home