1

I am just becoming familiar with serialization of objects in C#. I am wondering if the deserialization constructor is called INSTEAD OF the default constructor or IN ADDITION TO. If it is IN ADDITION TO, what order are these called in? For example:

[Serializable()]
public class ReadCache : ISerializable
{
    protected ArrayList notifiedURLs;

    // Default constructor
    public ReadCache()
    {
        notifiedURLs = new ArrayList();
    }

    // Deserialization constructor.
    public ReadCache(SerializationInfo info, StreamingContext ctxt)
    {
        //Get the values from info and assign them to the appropriate properties
        notifiedURLs = (ArrayList)info.GetValue("notifiedURLs", typeof(ArrayList));
    }
}
Doug
  • 5,116
  • 10
  • 33
  • 42

1 Answers1

2

No it will get called "instead" of the default - but you can initialize your list with something like this:

public ReadCache(SerializationInfo info, StreamingContext ctxt)
  : this()
{
    //Get the values from info and assign them to the appropriate properties
    notifiedURLs = (ArrayList)info.GetValue("notifiedURLs", typeof(ArrayList));
}

Please note the "... : this()" - syntax - but in your special case you don't have to!

Maheep
  • 5,539
  • 3
  • 28
  • 47
Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • Gotcha. When :this() is used, which code is executed first? The default constructor code or the deserialization constructor code? – Doug Mar 12 '12 at 06:56
  • 1
    the part in your "default"-constructor - think on it, not very useful otherwise isn't it? – Random Dev Mar 12 '12 at 06:59