C# Does Deserialization Constructor get called INSTEAD of the default constructor?
-
12-12-2019 - |
Question
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));
}
}
Solution
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!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow