Christophe Lambrechts
Christophe Lambrechts

Reputation: 538

Parameterless constructor for serialisation in combination with constructor who has default parameter, why does it work?

I have written some code that works great, but I don't understand why it works. I want to serialize a class, with let's say an integer. So here is the code of the class.

[Serializable]
public class TestClass
{
    public int Variable;
}

Now I want to have the possibility to create an instance of this class and setting the Variable right away. So I create a constructor and also a private default constructor for the serializer.

[Serializable]
public class TestClass
{
    public int Variable;

    private TestClass() {}

    public TestClass(int value)
    {
        Variable = value;
    }     
}

So in the code where I'm using this, default parameters become handy. So I applied that technique to the public constructor.

[Serializable]
public class TestClass
{
    public int Variable;

    private TestClass() {}

    public TestClass(int value = 0)
    {
        Variable = value;
    }     
}

And this all works fine, but why? Do we don't have now two constructors with the same definition? Even if I make the default constructor public, everything works fine. Once I remove the default constructor, it fails on serialisation. My first guess is that the XmlSerialize of .NET doesn't check for constructors with default parameters. But why does the compiler still allows this code?

I'm just curious on this, but also hope to learn and better understand how things work internally. Googling doesn't bring up anything useful, maybe just because I couldn't think about the right search terms.

Upvotes: 3

Views: 1475

Answers (1)

Chris Hannon
Chris Hannon

Reputation: 4134

The XmlSerializer specifically requires the class that it serializes to have a parameterless constructor, and if you put a breakpoint in your private constructor you'll notice that it's hit on deserialization. A constructor with default arguments is not a parameterless constructor, it is a constructor that takes parameters that are inserted for you by the compiler.

Overload resolution on a parameterless constructor vs a constructor with only default arguments (or any method in the same situation) comes down to specificity. Among the myriad of other overload resolution rules, the C# 4.0 language specification (7.5.3.2 Better function member) states:

Otherwise if all parameters of MP have a corresponding argument whereas default arguments need to be substituted for at least one optional parameter in MQ then MP is better than MQ.

From this information, we can simplify and ask a question about the constructors you have. When you say new TestClass(), which one should it pick?

  1. We have specified all of the arguments that we would like to pass to the call. In this case, it's zero.

  2. Is there a constructor with exactly zero arguments that is visible to the caller?

  3. If so, call it.

  4. If not, call the next best thing. In this case, it's your constructor that has default arguments.

Note that the XmlSerializer does not follow these rules. It knows specifically what it wants and will complain if you do not provide it.

Upvotes: 5

Related Questions