Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6850093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:04:53+00:00 2026-05-27T01:04:53+00:00

I have written some code that works great, but I don’t understand why it

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:04:54+00:00Added an answer on May 27, 2026 at 1:04 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some code to ensure that items on an order are all
I have an application written in C# that invokes some C code as well.
I have a c++ source code that was written in linux/unix environment by some
I have some old code written in C for 16-bit using Borland C++ that
I have written some C++ code that generates a std::vector. I also have a
I have written a good bit of code in python and it works great.
I have re-written my code after great help from some friendly stack overflow members
I have written some code in my VB.NET application to send an HTML e-mail
I have written some code for playing a .wav through my application. Now I
I have written some code to look at properties using reflection. I have retrieved

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.